mkdir Command : How to Create a Directory in Linux

Today we will learn how to create a directory in Linux operating system using mkdir command.

Let’s get started.

The mkdir command is derived from an ordinary word and that is Make Directory.

What we call a folder in Microsoft Windows is called a directory in Linux.

Features of mkdir Command

  • Make new Directories
  • Create Parent Directories
  • Set permissions for a new Directory
  • Display Output Message

First of all, let’s focus on some of the most important options that we can use with the mkdir.

OptionsExplanation
-m, --mode=MODESet file mode (as in chmod)
-p, --parentsMake parent directories as needed
-v, --verbosePrint a message for each created directory
--helpDisplay this help and exit
--versionVersion Information

Syntax:

You must follow the syntax given below to use the mkdir command.

mkdir [OPTION]... DIRECTORY...

1. Create a New Directory

To create new directory type the following command.

~$ mkdir data

Output:

~$ ls
data

2. Create Multiple Directories

To create multiple directories, pass the names of the directories to mkdir command.

In this example, I am creating three directories named data, data1, and data2.

~$ mkdir data data1 data2

Output:

~$ ls
data  data1  data2

There are other methods by which we can create multiple directories.

Method #1

You can specify the names of directories inside a curly bracket. Make sure that the directory names should be separated by commas.

Example:

~$ mkdir {data1,data2,data3}
~$ ls
data1  data2  data3

Method #2

You can also use the following method to create multiple directories.

~$ mkdir data{1,2,3,4,5}

Output:

~$ ls
data1  data2  data3  data4  data5

3. Display Output Message for each created Directory

To display the output message for each created directory pass the -v option to mkdir.

Let’s take some examples:

Task #1 Create a directory named data.

~$ mkdir -v data
mkdir: created directory 'data'
Output Message

Task #2 Create multiple directories named data1, data2, and data3.

~$ mkdir -v data1 data2 data3
mkdir: created directory 'data1'
mkdir: created directory 'data2'
mkdir: created directory 'data3'

You can also use the long option --verbose.

~$ mkdir --verbose data

4. Create parent directories using mkdir Command

To create parent directories pass the -p option to mkdir command.

Now you must be wondering what is Parent Directory. Let me explain you with an example:

I want to create a directory named data3 inside data1/data2/.

So data1 and data2 are parent directories of data3.

Parent Directories

Assuming the parent directories are not available.

So to create the entire directory path i.e. data1/data2/data3 type the following command.

mkdir -p data1/data2/data3

Output:

~$ ls -R
.:
data1

./data1:
data2

./data1/data2:
data3

./data1/data2/data3:

If you run the above command without -p, Following error will come.

~$ mkdir data1/data2/data3
mkdir: cannot create directory ‘data1/data2/data3’: No such file or directory

To get the output message for each created directory type the following command.

~$ mkdir -pv data1/data2/data3
mkdir: created directory 'data1'
mkdir: created directory 'data1/data2'
mkdir: created directory 'data1/data2/data3'

You can also use the long option --parents.

~$ mkdir --parents data1/data2/data3

5. Set Permissions to Directories

By default, mkdir command assigns 755 permissions to a new directory.

Refer to the sample output below.

Where:

  • 7 : Owner is allowed to Read, Write, and Execute.
  • 5 : Group is allowed to Read & Execute.
  • 5 : Others are also allowed to Read & Execute.
~/data$ ls -ld data/
drwxr-xr-x 2 ubuntu ubuntu 4096 Jun 25 01:30 data/

But you can use the mkdir command to set the desired permission for the new directory.

To do so pass the -m option to mkdir.

Here I am assigning Full Permissions to the directory named newdir.

~$ mkdir -m 777 newdir

Note: 777 – Owner, Group & Others are allowed to Read, Write, and Execute.

Output:

~$ ls -ld newdir/
drwxrwxrwx 2 ubuntu ubuntu 4096 Jun 25 01:31 newdir/

OR you can assign permissions using alphabetical way.

~$ mkdir -m a=rwx newdir

You can also use the Long Option --mode

~$ mkdir --mode 777 newdir

6. Solution : cannot create directory : Permission denied

The Linux operating system has some file systems in which you need root access to create a file/directory.

For example, when I was creating a directory inside the /var file system as a normal user, I received the following error.

~$ cd /var/
/var$ mkdir test
mkdir: cannot create directory ‘test’: Permission denied

To overcome this problem you have to run the command using sudo.

Example:

/var$ sudo mkdir -v test
[sudo] password for ubuntu:
mkdir: created directory 'test'

7. Help/Manual page access

Use the following commands to access the Manual Page/Help Page of mkdir command.

~$ mkdir --help
~$ man mkdir

8. Check the version of mkdir command

Check the mkdir command version using the following command.

~$ mkdir --version

Infographic

Refer to this Infographic for complete mkdir command options.

mkdir command

You can visit at following websites to get more information on mkdir command.

Conclusion

I hope you have learned something from this article.

I have tried my best to include all the features of mkdir command in this guide.

Now I’d like to hear your thoughts.

Was this guide useful to you?

Or maybe you have some queries.

Have I not included any command in this guide?

If you like our content, please consider buying us a coffee.

Buy Me A Coffee

We are thankful for your never ending support.

Leave a Comment