mkdir command in Linux/Unix With Examples

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

To find out your current working directory run:

In my case, this happens to be /home/jamie as shown in the following image.

pwd-command

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:

You can verify this using the ls -l command.

pwd-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.

In our case, we shall create 3 more directories and display the output.

Output

mkdir-verbose

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:

The above command creates dir2 inside dir1 and dir3 in dir2.

Example

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.

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

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.

To verify that we have created the directory with the assigned values execute:

Output

docs-rwx-permissions

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.

Output

docs-rwx-permissions

Getting the version for mkdir command

To obtain the version of mkdir command in your system execute:

Output

mkdir-version

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.

By admin

Leave a Reply