mkdir Command
Beginner File Management man(1)Create new directories
👁 10 views
📅 Updated: Mar 15, 2026
SYNTAX
mkdir [OPTION]... DIRECTORY...
What Does mkdir Do?
The mkdir command creates new directories (folders) in the Linux filesystem. It is one of the core file management commands used constantly in daily work.
mkdir can create single directories, multiple directories at once, and entire directory trees in one command using the -p flag. It supports setting permissions during creation, which is important for security-sensitive directories.
The command respects the current umask setting for default permissions. For directories that need specific access rights (like web server directories or shared folders), use the -m flag to set exact permissions at creation time rather than relying on chmod afterward.
mkdir can create single directories, multiple directories at once, and entire directory trees in one command using the -p flag. It supports setting permissions during creation, which is important for security-sensitive directories.
The command respects the current umask setting for default permissions. For directories that need specific access rights (like web server directories or shared folders), use the -m flag to set exact permissions at creation time rather than relying on chmod afterward.
Options & Flags
| Option | Description | Example |
|---|---|---|
| -p | Create parent directories as needed (no error if existing) | mkdir -p /var/www/html/assets/css |
| -m | Set permission mode during creation | mkdir -m 755 /var/www/newsite |
| -v | Verbose — print each directory as it is created | mkdir -pv project/{src,tests,docs} |
| -Z | Set SELinux security context | mkdir -Z unconfined_u:object_r:httpd_sys_content_t:s0 /var/www/site |
| --context | Like -Z but for a specified SELinux context | mkdir --context=system_u:object_r:tmp_t:s0 /tmp/secure |
| multiple | Create multiple directories in one command | mkdir dir1 dir2 dir3 |
Practical Examples
#1 Create a simple directory
Creates a single directory called projects in the current location.
$ mkdir projects#2 Create nested directory tree
Creates the entire directory structure including all parent directories. Brace expansion creates css, js, and images subdirectories.
$ mkdir -p /var/www/myapp/public/assets/{css,js,images}#3 Create with specific permissions
Creates the .ssh directory with permissions allowing only the owner to read, write, and execute.
$ mkdir -m 700 ~/.ssh#4 Create project scaffold
Creates a complete project directory structure in one command with verbose output.
$ mkdir -pv myproject/{src/{controllers,models,views},tests,config,public/{css,js}}
Output:
mkdir: created directory 'myproject'
mkdir: created directory 'myproject/src'
mkdir: created directory 'myproject/src/controllers'
#5 Create directory for each date
Creates a date-organized backup directory like /backup/2025/03/14.
$ mkdir -p /backup/$(date +%Y/%m/%d)#6 Create multiple directories
Creates four directories at once in the current location.
$ mkdir logs tmp cache uploadsTips & Best Practices
Brace expansion for scaffolding: Use bash brace expansion with -p to create complex directory trees: mkdir -p app/{frontend/{components,pages,styles},backend/{routes,middleware,models}}
Check umask before creating shared directories: The default umask (usually 022) affects permissions. For shared directories, explicitly set permissions with -m to avoid access issues.
install command alternative: The install -d command combines mkdir and chmod in one step: install -d -m 755 -o www-data -g www-data /var/www/site
Frequently Asked Questions
How do I create a directory and all parent directories?
Use mkdir -p /full/path/to/directory. The -p flag creates any missing parent directories and does not error if the directory already exists.
How do I set permissions when creating a directory?
Use mkdir -m followed by the permission mode: mkdir -m 750 /var/www/private. This is more secure than creating first and then running chmod.
Why does mkdir fail with "Permission denied"?
You need write permission in the parent directory to create a new subdirectory. Use sudo if creating in system directories like /var, /etc, or /opt.
Related Commands
More File Management Commands
Master Linux with Professional eBooks
Curated IT eBooks covering Linux, DevOps, Cloud, and more
Browse Books →