mkdir means make directories. mkdir command is a easy command and having very less options to remember. To create directories in Linux systems we use mkdir command. We can create single directory, multiple directories at once and nested directories also. While creating directory if it already available then it shows directory exists otherwise new directory will be created. With mkdir command we can set permissions while creating directories. We will discuss all the available options in mkdir command below.
mkdir command syntax:
mkdir [option] <Directory>
Create a new directory:
To create a directory called Student_data use below command.
# mkdir Student_data
Create multiple directories at once:
We can create multiple directories by using one mkdir command. To create multiple directories mention the directory names separated by space in mkdir command as below.
# mkdir Exam_details College_details Branch_details
Set permissions to directories while creating:
With -m (mode) option we can set the required permissions to that directory while creating. This option is useful to set permissions at the time of creating. In below command we are setting 777 permission (means owner of the file, all groups, other users can access this file) to the file. You can update the permissions like 755, 644 etc. Use ls -ld (learn more about ls command) command to check the newly created directory permissions. In our case it is Student_data
# mkdir Student_data -m=777 # mkdir Student_data --mode=777 # ls -ld Student_data
Create nested directories at once:
To create nested directories in one command we can use -p or –parents option in mkdir command. While creating nested directories if any of the nested directory exists no error will be displayed. It will create other directories. You can use any of the commands below.
In below command we are creating student_data directory inside /home/user_1/college/branch directory.
# mkdir -p /home/user_1/college/branch/student_data/ # mkdir --parents /home/user_1/college/branch/student_data/
# mkdir -pv /home/user_1/college/branch/student_data/ mkdir: created directory '/home/user_1/college/branch/student_data/'
Display directory creation status:
By default mkdir command wont display the output after successfully creating directories. If you want to display the output of the command use -v or –verbose option. This -v or –verbose option we can use with any option in the mkdir command.
# mkdir -v Student_datta # mkdir -v Exam_details College_details Branch_details
Add selinux security context for the directories:
With -Z or –context option we can enable selinux (Security Enhanced Linux) security context to the new directory to default type
# mkdir -Z Student_data # mkdir --context Student_data
Know the version for mkdi command:
To check the version of mkdir command use –version option.
# mkdir --version
Relative paths and Absolute paths:
While creating a new directory we can the concept of relative paths and absolute paths. Lets say you are in /home directory and if you want to create a new directory called “David”. Lets how we can use relative paths, absolute paths while creating the “David” directory in /home path
# mkdir /home/David
# mkdir David
While creating directories if you want to use absolute path means you have to provide the full path (In our case it is /home/David) of the directory.
If you want to use relative path then you have to cd to the path (In our case it is /home) where you want to create the new directory and create the directory.