The Linux directory structure is such that at any given point in the terminal, you are working inside a particular directory. The Linux directory hierarchy starts at the top with the root (/) directory and branches into several subdirectories as you go down the hierarchy tree.
In this guide, we will focus on
mkdir – short for make directory – is a command used for creating directories in Linux/Unix systems and assign other attributes as well.
Table of Contents
Create a directory using the mkdir command
To create a directory using the mkdir command use the syntax as shown below
Copymkdir [OPTIONS] directory_name
To find out your current working directory run:
In my case, this happens to be
/home/jamie as shown in the following image.
Now we going to navigate to the ‘Documents’ directory
And create 3 directories – directory1, directory2, and directory3.
The syntax for creating directories without arguments using the mkdir command is:
Therefore to create all the three directories, we are going to run:
Copymkdir directory1 directory2 directory3
You can verify this using the
ls -l command.
Print or Display verbose output using -v option
If you desire to print or display the operation of mkdir command, use the
-v option as shown below.
Copymkdir -v directory_name
In our case, we shall create 3 more directories and display the output.
Copymkdir -v directory4 directory5 directory6
As shown above, the verbose output has been printed showing the operations that have been carried out.
Create subdirectories using the -p option
mkdir -p command allows you to create nested directories or parent directories only if they do not exist.
When the above command is repeated, nothing will happen and no error will be reported. It is thus said to be an idempotent operation.
If you have a directory “/dir1” and you run following command:
Copymkdir -p /dir1/dir2/dir3
The above command creates dir2 inside dir1 and dir3 in dir2.
Copymkdir -p linux/ditros/debian
This creates “debian” directory inside “distros” folder and “distros” inside “linux” directory.
This can be confirmed by navigating to the debian/distros/debian path and running
Confirming the current working directory with
Assigning permissions using the -m option
By default , the
mkdir assigns rwx r_x r_x permission or simpy 755 in Octal format. If you want to assign different values use the
mkdir -m and the corresponding permissions.
Copymkdir -m octal_value directory_name
For example, we are going to create a directory called docs and assign it all the permissions (octal value 777).
To accomplish that, we shall execute the following command.
Copymkdir -m 777 docs
To verify that we have created the directory with the assigned values execute:
As seen above the docs directory has all the permissions assigned to it and is highlighted in green.
Accessing manpages for mkdir command
To access mkdir command man pages and learn more about the command usage, execute the command below.
Getting the version for mkdir command
To obtain the version of mkdir command in your system execute:
In this guide, we dissected the mkdir command and gave example usages of the command. We do hope that you have found this useful especially if you are a beginner and learning the ropes in Linux. Feel free to try out the commands and get back to us. Your feedback is most welcome.