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
command. mkdir
– short for make directory – is a command used for creating directories in Linux/Unix systems and assign other attributes as well.
Create a directory using the mkdir command
To create a directory using the mkdir command use the syntax as shown below
Syntax
1 |
mkdir [OPTIONS] directory_name |
To find out your current working directory run:
1 |
pwd |
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:
1 |
mkdir directory_name |
Therefore to create all the three directories, we are going to run:
1 |
mkdir 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.
1 |
mkdir -v directory_name |
In our case, we shall create 3 more directories and display the output.
1 |
mkdir -v directory4 directory5 directory6 |
Output
As shown above, the verbose output has been printed showing the operations that have been carried out.
Create subdirectories using the -p option
The 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:
1 |
mkdir -p /dir1/dir2/dir3 |
The above command creates dir2 inside dir1 and dir3 in dir2.
Example
1 |
mkdir -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 pwd
command.
Confirming the current working directory with pwd
command.
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.
Syntax
1 |
mkdir -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.
1 |
mkdir -m 777 docs |
To verify that we have created the directory with the assigned values execute:
1 |
ls -l |
Output
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.
1 |
man mkdir |
Output
Getting the version for mkdir command
To obtain the version of mkdir command in your system execute:
1 |
mkdir --version |
Output
Conclusion
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.