The “mkdir” Command in Linux [6+ Practical Examples]

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

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

Note: In the above syntax OPTION enclosed by a square bracket and followed by 3 dots represents that multiple options can be utilized at the same time. Moreover, 3 dots after DIRECTORY suggests that you can specify multiple directories to create multiple.

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.)
NB: The options in Linux CLI(Command Line Interface) are all case-sensitive, So be cautious while using them.

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.Creating a new directory using the mkdir command in Linux.

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

Installing the tree command in LinuxFinally, 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.Verbose mode of the mkdir command in Linux.

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.Creating a new directory on a specific location using the mkdir command in Linux.


Similar Readings


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.Creating three new directories using the mkdir command in Linux.

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.Creating a new directory along with its parent directories using the mkdir command in Linux.

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.Setting permission while creating a new directory using the mkdir command in Linux.


Similar Readings


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.The desired model of the directories hierarchy.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({}).Creating the desired model directory hierarchy using the mkdir command in Linux.

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

Rate this post
Md. Ashakul Islam Sowad

Hi, I am Md. Ashakul Islam Sowad from Dhaka, Bangladesh. I have completed my undergraduate degree in Biomedical Engineering from the Bangladesh University of Engineering and Technology (BUET). I love to watch football and play video games in my free time. Here, I am working as a Linux Content Developer Executive. Furthermore, as a Linux enthusiast, I am always learning new things about Linux-based systems and I’ll be sharing them here. Read Full Bio

Leave a Comment