
You can create new directory on your Linux systems using command line or using UI of file manager. The mkdir
command allows you to create a new directories (folders) on your Linux systems. This guide explains use of mkdir
command with examples.
Linux mkdir Command Syntax
The basic syntax for the mkdir command is as below:
mkdir [OPTION] [DIRECTORY]
You can pass more then one directory names as its arguments.
Create a New Directory
In Linux systems, you can pass directory names as arguments along with mkdir
command. For instance, to create a new directory test_dir
run below command:
mkdir test_dir
To verify that the directory was created by listing the contents using the ls
command:
ls -l
drwxrwxr-x 2 tecnstuff tecnstuff 4096 Dec 29 01:12 test_dir
When you are in current working directory and run ls
command it will just show directory name without full path. To change current working directory to test_dir
, use cd command.
You can create directory to other location using absolute or relative path of parent directory.
mkdir /var/www/test_dir
If user will not have sufficient permission then it will throw Permission denied
error message:
mkdir /root/test_dir
mkdir: cannot create directory '/root/test_dir': Permission denied
Create Parent Directories
You can also create parent directory for any other directory. To create parent directories, use mkdir
command with the -p
option.
For instance, you want to create directory /home/tecnstuff/Documents/Tec/Stuff
mkdir /home/tecnstuff/Documents/Tec/Stuff
You will get an error as shown below if any of the parent directories don’t exist:
mkdir: cannot create directory '/home/tecnstuff/Documents/Tec/Stuff': No such file or directory
You can create missing parent directories by invoking mkdir
command with -p
option:
mkdir -p /home/tecnstuff/Documents/Tec/Stuff
The -p
option doesn’t effect if directory exists, it will create the directory only if it doesn’t exist.
If you will try to create directory which is exists without -p
option then it will show File exists
error message:
mkdir test_dir
mkdir: cannot create directory 'test_dir': File exists
How to Create Multiple Directories
If you want multiple directories within a single command, just pass directories’ names as arguments by space
separated:
mkdir test1 test2 test3
Set Permissions while Creating a Directory
You can set specific directory permissions while creating directory using mkdir command. Use -m
(-mode)
option along with mkdir
command and syntax will be same as chmod
command:
For example, we are creating a directory with 700
permission so directory will be accessible only by creator of directory.
mkdir -m 700 test_dir
If the mkdir used without -m
option then directories will have 775
or 755
permissions.
Conclusion
The mkdir
command in Linux is basic command and used to create new directories. You also can create multiple directories in a single command. To get more details about mkdir, visit the mkdir man page.
If you have questions or feedback, please leave a comment below.
Leave a Reply