Overview
mkdir (make directory) is the fundamental command used to create new directories (folders) in Linux.
Beyond simply creating a single folder, it allows you to generate deep hierarchies in a single step and set access permissions (modes) at the moment of creation. Compared to GUI folder creation, it offers overwhelming efficiency when automating tasks via scripts or generating large numbers of directories at once.
Specifications (Arguments and Options)
Syntax
mkdir [options] [directory_name...]
Main Options
| Option | Description |
-p | Automatically creates any missing parent directories in the specified path (parents). Frequently used to avoid “No such file or directory” errors. |
-m [mode] | Sets the directory permissions (e.g., 755, 700) numerically at the time of creation (mode). |
-v | Displays a message for every directory created (verbose). |
Basic Usage
Creating a Directory
The simplest usage is providing the name of the directory you wish to create.
mkdir workspace
Execution Result Example:
(No output is shown if successful. Verify using the ls command)
ls -F
workspace/
Practical Commands
1. Creating Deep Hierarchies at Once (-p)
Normally, running mkdir dir1/dir2/dir3 results in an error if dir1 or dir2 do not already exist. Using the -p option creates the entire parent hierarchy recursively.
# Automatically creates the 'logs' and 'app' directories if they don't exist
mkdir -p logs/app/2026
2. Creating Multiple Subdirectories (Brace Expansion)
This is useful when you need to set up a standard project structure. It utilizes the shell’s brace expansion {A,B,C} feature.
# Creates src, bin, and doc directories inside the 'project' folder simultaneously
mkdir -p project/{src,bin,doc}
Execution Result Example:
tree project
project
├── bin
├── doc
└── src
3. Creating a Private Directory (-m)
When creating a directory for sensitive information, you can skip the extra chmod step by setting permissions immediately.
# Create a directory accessible only by the owner (700)
mkdir -m 700 private_data
Execution Result Example:
ls -ld private_data
drwx------ 2 user user 4096 Jan 16 10:00 private_data
Note: For directories, the execution (x) permission is required to enter the folder. Therefore, 700 is standard rather than 400.
Customization Points
- Error Suppression with
-p: The-poption has the side effect of not throwing an error if the directory already exists. This makes it ideal for scripts that ensure a directory “is there.” - Permission Settings (
-m): Use-m 755for public web content and-m 700for strictly private data.
Important Notes
- Directory Permissions: Unlike files, if a directory lacks the execution (x) permission, you cannot
cdinto it or list its contents withls. Creating a folder with-m 400results in a locked folder that even the owner cannot enter without achmodfix. - Write Permissions: To create a directory, you must have write (w) permissions in the parent directory. Creating folders in system paths like
/usr/local/requiressudo mkdir.
Advanced Application
Creating a Backup Directory with the Current Date
Use command substitution to create a folder named with the current date (YYYY-MM-DD format).
mkdir -p backup/$(date +%F)
Conclusion
mkdir is a simple command, but the -p option significantly changes your workflow efficiency.
Reminder: Always ensure the owner has execution (x) permissions for the directory.Using mkdir -p is the standard “best practice” in the field to avoid errors and ensure reliability.
Ideal Use Cases: General folder creation and hierarchical setup in scripts.
Key Options: Use -p for hierarchies and -m for specific permissions.
