How to Create Directory in Linux: Simple mkdir Command Guide
To create a directory in Linux, use the
mkdir command followed by the directory name. For example, mkdir myfolder creates a new directory named myfolder in the current location.Syntax
The basic syntax to create a directory is:
mkdir [options] directory_name
Here, mkdir is the command, directory_name is the name of the directory you want to create, and [options] are optional flags to modify behavior.
Common options include:
-p: Create parent directories as needed.-v: Show messages for each created directory.
bash
mkdir [options] directory_name
Example
This example creates a directory named projects in the current folder and then creates nested directories projects/python/scripts using the -p option.
bash
mkdir projects mkdir -p projects/python/scripts ls -R projects
Output
projects:
python
projects/python:
scripts
projects/python/scripts:
Common Pitfalls
Some common mistakes when creating directories include:
- Trying to create a directory that already exists without
-p, which causes an error. - Not having permission to create directories in the target location.
- Using spaces in directory names without quotes, causing errors.
Example of wrong and right usage:
bash
mkdir my folder
mkdir "my folder"Output
mkdir: cannot create directory ‘my’: No such file or directory
(no error for second command, directory created)
Quick Reference
| Command | Description |
|---|---|
| mkdir directory_name | Create a single directory |
| mkdir -p path/to/directory | Create nested directories, including parents |
| mkdir -v directory_name | Show message for each directory created |
| mkdir -m 755 directory_name | Set permissions while creating directory |
Key Takeaways
Use
mkdir directory_name to create a new directory in Linux.Add
-p to create nested directories in one command without errors.Quote directory names with spaces to avoid errors.
Check permissions if directory creation fails.
Use
-v to see confirmation messages when creating directories.