The mkdir command is one of the most helpful commands in Linux. You can use this to create one or more directories from the terminal. Therefore the mkdir command can be pretty time efficient while creating large numbers of directories. Hence, as a Linux enthusiast, you should learn in detail about the mkdir command in Linux.
A. Description
In Linux, mkdir is the abbreviation for make directory. As the name suggests the mkdir command can be used to create one or more directories.
B. Syntax
The syntax for the mkdir command is pretty simple which is the command itself followed by some specific options and the directory name you want to create.
mkdir [OPTION]... DIRECTORY...
C. Options
Many useful options are available for the mkdir command. I have listed some of the most used ones here. However, if you want to know more about options for the mkdir command you can always check the man page for the mkdir command.
man mkdir
Useful Options
- -m (Sets permission attributes)
- -p, –parents (Creates the necessary parent directories if required)
- -v, –verbose (Prints message of what is being performed.)
Practical Examples of the “mkdir” Command in Linux
The mkdir command in Linux is an easy-to-use command used to make directories. Some of the most helpful applications of the mkdir command have been illustrated below.
Example 1: Creating a New Directory Using the “mkdir” Command
You can create a new directory using the mkdir command. To create a new directory you should follow the below procedure. Here, I have created a “Test” directory on my desktop.
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
mkdir Test
➌ Now, press the ENTER button.
Output >
In the below image, you can visualize that a new directory named Test has been created in my current working directory.
Note: The tree command lists the contents of a directory recursively in a tree-like configuration. If you’re using the tree command for the first time in Ubuntu you will have to install the command. You can do it simply by running the below command:
sudo apt install tree
Finally, provide the user password and you’re good to go with the tree command.
A. Printing Message Of Creating Directory
You can print the message of what you’ve done by adding the -v option. To do so follow the below process.
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
mkdir -v HI
➌ Now, press the ENTER button.
Output >
In the below image, you can visualize that a new directory named HI has been created with a printed message of that task in my terminal.
Example 2: Creating Directories in a Specific Location
You can create a new directory on a specific location using the mkdir command. Currently, my terminal is open in the Desktop folder. Here, I have created a new directory named “Temporary” on my home directory from my Desktop. To do the same follow the below process.
Steps to Follow >
➊ At first open the Ubuntu Terminal on your desktop.
You can open Ubuntu Terminal from a specific directory. To learn details about that click here.
➋ Type the following command in the command prompt:
mkdir /home/sowad/Temporary
➌ Now, press the ENTER button.
Output >
In the below image, you can visualize that a new directory named Temporary has been created in my home directory.
Similar Readings
- The “diff” Command in Linux [11 Practical Examples]
- The “cmp” Command in Linux [7 Practical Examples]
- The “comm” Command in Linux [10 Practical Examples]
Example 3: Creating Multiple Directories Using the “mkdir” Command
You can create multiple directories using the mkdir command. To create multiple directories you should follow the below procedure. Here, I have created three directories named “Animals”, ”Birds” and ”Flowers” on my desktop.
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
mkdir Animals Birds Flowers
➌ Now, press the ENTER button.
Output >
In the below image, you can visualize that three new desired directories have been created in my current working directory.
Example 4: Creating Directories Along With Parent Directory
In example 2, I explained how you can create a new directory in a specific location or directory using the mkdir command. However, if the parent directory doesn’t exist, first you have to create the parent directory then you’ll be able to create the desired directory in that specific location.
Fortunately, in Linux with the help of the -p option, you can create new directories along with the necessary parent directories using the mkdir command by following the below process.
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
mkdir -p Sports/Football/League/EPL/Arsenal
➌ Now, press the ENTER button.
Output >
In the below image, you can visualize that the desired directory with the necessary parent directories has been created in my current working directory.
Example 5: Setting Permissions While Creating Directories
You can set the permissions of the directory while you are creating it using the mkdir command. You have to use the -m option to do so. However, you have to remember some modes/arguments for the -m option. I have listed them below:
- u represents user (u+x will empower the user with executable permission.)
- g represents groups (g-w will revoke the modification power of members of the group.)
- o represents others (o+r will empower others to read the contents.)
- a represents all (a=r will grant everyone accessing power however it will revoke everyone from write and execution permissions.)
Now, I will create a new directory named “Permission” using the mkdir command and set its permission to only read. You can do the same by following the below process.
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
mkdir -m a=r Permission
➌ Now, press the ENTER button.
Output >
In the below image, you can visualize that the desired directory named “Permission” with only read permission has been created in my current working directory.
Similar Readings
- The “ln” Command in Linux [6 Practical Examples]
- The “lsof” Command in Linux [8 Practical Examples]
- The “tar” Command in Linux [12 Practical Examples]
Example 6: Miscellaneous Application of the “mkdir” Command in Linux Using Brace Expansion
In this example, I will combine the concepts of previously illustrated examples. I will make the following directory hierarchy model in my current directory using the mkdir command in Linux.To do the same you can go along with the following process.
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
mkdir -p Schedule/{Monday,Tuesday,Wednesday,Thursday,Friday}/{Morning,Noon,Night}
➌ Now, press the ENTER button.
Output >
In the below image, you can visualize that I have generated the desired directory hierarchy model using the mkdir command along with the -p option and a special character, Curly Brace({}).
Conclusion
In this article, I have tried to present you with the applications and usefulness of the mkdir command in Linux. Hope that you’ll be able to explore more things with the help of these practical examples and make many directories!
Similar Readings
- The “chgrp” Command in Linux [7 Practical Examples]
- The “file” Command in Linux [9+ Practical Examples]
- The “fsck” Command in Linux [7 Practical Examples]
- The “patch” Command in Linux [4 Practical Examples]
- The “rmdir” Command in Linux [7 Practical Examples]
- The “stat” Command in Linux [9 Practical Examples]
FUNDAMENTALS A Complete Guide for Beginners