Overview
The mkdir (make directory) command creates new directories in the filesystem. While seemingly simple, mkdir offers powerful options for creating directory structures, setting permissions, handling errors, and providing verbose output. Understanding mkdir's capabilities is fundamental to filesystem management and scripting in Linux/UNIX environments.
Syntax: mkdir [OPTIONS] directory_name...
The mkdir command creates directories with names specified as arguments. It can create single directories, multiple directories at once, or entire directory hierarchies with nested subdirectories.
Example 1
Creating a Single Directory
$ ls
file1.txt file2.txt
$ mkdir projects
$ ls -l
drwxr-xr-x 2 craig staff 64 Nov 7 12:00 projects
-rw-r--r-- 1 craig staff 42 Nov 7 11:55 file1.txt
-rw-r--r-- 1 craig staff 38 Nov 7 11:56 file2.txt
$ cd projects
$ pwd
/home/craig/projects
What's Happening:
The most basic use of mkdir creates a single directory in the current location. The new directory is created with default permissions (typically 755, modified by your umask). The 'd' at the start of the permissions indicates it's a directory. You can then navigate into it using cd.
Default Permissions: New directories typically get rwxr-xr-x (755) permissions, but this is affected by the umask setting. Check with: umask
Example 2
Creating Multiple Directories Simultaneously
$ mkdir dir1 dir2 dir3 dir4
$ ls -l
drwxr-xr-x 2 craig staff 64 Nov 7 12:05 dir1
drwxr-xr-x 2 craig staff 64 Nov 7 12:05 dir2
drwxr-xr-x 2 craig staff 64 Nov 7 12:05 dir3
drwxr-xr-x 2 craig staff 64 Nov 7 12:05 dir4
# Create directories with descriptive names:
$ mkdir backups documents downloads music photos videos
# Using brace expansion:
$ mkdir project{1,2,3,4,5}
$ ls
project1 project2 project3 project4 project5
# Sequential numbering:
$ mkdir test{01..10}
$ ls
test01 test02 test03 test04 test05 test06 test07 test08 test09 test10
What's Happening:
Mkdir accepts multiple directory names as arguments, creating all of them in a single command. This is much more efficient than running mkdir repeatedly. Bash brace expansion provides powerful shortcuts for creating numbered or patterned directory names. The syntax {1,2,3} creates specific names, while {01..10} creates sequential ranges with zero-padding.
Pro Tip: Brace expansion works before mkdir runs, so mkdir {a,b,c}{1,2} creates a1, a2, b1, b2, c1, c2.
Example 3
Creating Parent Directories with -p (Path)
# Without -p, this fails if parent doesn't exist:
$ mkdir projects/web/frontend/components
mkdir: cannot create directory 'projects/web/frontend/components': No such file or directory
# With -p, creates entire path:
$ mkdir -p projects/web/frontend/components
$ tree projects
projects
βββ web
βββ frontend
βββ components
3 directories, 0 files
# Create complex directory structure:
$ mkdir -p company/{hr,it,sales}/{2023,2024,2025}/{q1,q2,q3,q4}
$ tree company
company
βββ hr
β βββ 2023
β β βββ q1
β β βββ q2
β β βββ q3
β β βββ q4
β βββ 2024 [same structure]
β βββ 2025 [same structure]
βββ it [same structure]
βββ sales [same structure]
# Also safe if directory already exists:
$ mkdir -p projects/web
# No error, does nothing
What's Happening:
The -p (parents) flag is one of mkdir's most useful options. It creates any missing parent directories in the path automatically. Without -p, mkdir fails if any parent directory doesn't exist. Additionally, -p makes mkdir idempotentβit won't error if the directory already exists, making it perfect for scripts.
Scripting Essential: Always use mkdir -p in scripts to avoid failures from directories that might already exist.
Example 4
Setting Permissions with -m (Mode)
# Create directory with specific permissions:
$ mkdir -m 700 private_data
$ ls -ld private_data
drwx------ 2 craig staff 64 Nov 7 12:10 private_data
# Only owner can read, write, execute
# Create with group write permissions:
$ mkdir -m 770 shared_team
$ ls -ld shared_team
drwxrwx--- 2 craig staff 64 Nov 7 12:11 shared_team
# Owner and group have full access
# Public read-only directory:
$ mkdir -m 755 public_docs
$ ls -ld public_docs
drwxr-xr-x 2 craig staff 64 Nov 7 12:12 public_docs
# Symbolic mode:
$ mkdir -m u=rwx,g=rx,o=rx standard_dir
$ ls -ld standard_dir
drwxr-xr-x 2 craig staff 64 Nov 7 12:13 standard_dir
# Create private directory with parents:
$ mkdir -pm 700 ~/.config/app/data
$ ls -ld ~/.config/app/data
drwx------ 2 craig staff 64 Nov 7 12:14 /home/craig/.config/app/data
What's Happening:
The -m (mode) flag sets permissions during directory creation, avoiding a separate chmod command. Permissions can be specified in octal (like 755) or symbolic notation (u=rwx,g=rx,o=rx). This is more secure than creating a directory and then changing permissions, as there's no window where the directory has default permissions. The -m flag can be combined with -p.
Security Note: When creating sensitive directories, always use mkdir -m 700 to ensure they're immediately protected.