How to Use mkdir Command in Linux: Syntax and Examples
Use the
mkdir command in Linux to create new directories by typing mkdir directory_name. You can create multiple directories at once or nested directories using options like -p.Syntax
The basic syntax of the mkdir command is:
mkdir [options] directory_name
Here, directory_name is the name of the directory you want to create. Options modify the behavior, such as creating parent directories.
bash
mkdir [options] directory_name
Example
This example shows how to create a single directory, multiple directories, and nested directories using mkdir.
bash
mkdir myfolder mkdir folder1 folder2 folder3 mkdir -p parent/child/grandchild
Common Pitfalls
Common mistakes include trying to create a nested directory without the -p option, which causes an error if parent directories don't exist. Also, forgetting permissions can cause failure.
bash
mkdir parent/child # Error: No such file or directory mkdir -p parent/child # Correct: creates all needed directories
Output
mkdir: cannot create directory ‘parent/child’: No such file or directory
Quick Reference
| Option | Description |
|---|---|
| -p | Create parent directories as needed |
| -v | Show a message for each created directory |
| -m mode | Set permissions for the new directory |
| --help | Display help information |
Key Takeaways
Use
mkdir directory_name to create a single directory.Use
mkdir -p path/to/directory to create nested directories in one command.Multiple directories can be created at once by listing them after
mkdir.Without
-p, creating nested directories fails if parents don't exist.Use
-v to see confirmation messages when directories are created.