How to Create Directory in Linux Using Mkdir Command

Create Directory in Linux

In Linux operating system, we can create new Directories/folders in two ways. As let’s assume we don’t have any panel, and we need to do this by CLI. So we are using mkdir command in this blog.

  • With the help of File Manager (GUI)
  • mkdir command (Command-line Utility).

In this tutorial, We will learn how to create a directory in Linux with mkdir command.

Read Also: How to install Docker on Ubuntu 20.04?

Linux mkdir Command Syntax

Let’s see the syntax of mkdir command:

mkdir [OPTION] [DIRECTORY]

How to Create a New Directory

In a Linux system, to create a new directory, simply pass the name of the directory which you want to create as an argument. We can pass multiple arguments to create multiple directories:

mkdir Testdir

Run the below command to check the directory.

ls -l
drwxrwxr-x 3 username username 6527 Dec 20 05:15 Testdir

While creating a new directory, if a full path is not specified with mkdir command then it will create the new directory in the current working directory.

To create the new directory at another location then specify the path with mkdir command. In the below, we will create a new directory in the /home/testdir

mkdir /home/testdir

The -v {--verbose} attribute is used with mkdir command to print message.

mkdir -v Testdir
mkdir: created directory 'testdir'

How to Create Parent Directories

In the directory tree, A parent directory is a directory that is above another directory. In a Linux system, we can’t directly create parent directories. To create parent directories we use -p option.

In the below example, we are trying to create directories like this /home/linuxpanda/video/movie :

mkdir /home/linuxpanda/video/movie

It will show an error if any of the parent directories don’t exist.

mkdir: cannot create directory '/home/linuxpanda/video/movie : No such file or directory

Now we have two options, create parent directories one by one or simply use -p attribute with the mkdir command. It will create all the missing directories themselves.

mkdir -p /home/linuxpanda/video/movie

If we try to create a directory that already exists without using -p attribute with mkdir command, then it will show an error File exists. If we use -p attribute then it will forcefully create the directory.

mkdir Testdir
mkdir: cannot create directory 'Testdir': File exists

How to Set Permissions when Creating a Directory

While creating the directory If we want to assign specific permission, we can use -m attribute with mkdir command to assign the permission. The default permission allowed by the Linux system for file 644 and Directory 755 according to the umask value.  

mkdir -m 771 Testdir

How to Create Multiple Directories

We can create multiple directories as below command. Just put the space between directory names.

mkdir Testdir1 Testdir2 Testdir3

The mkdir command also allows us to create a complex directory tree with one command:

mkdir -p Music/{English/Pitbull,Bollywood,Rock,Pop/{Bruno_Mars,Justin_Bieber},Classical/Old/90_Song}

Tree structure for the above command:

Music/
|-- Classical
|   `-- Old
|       `-- 90_Song
|-- Rock
|-- Bollywood
|-- English
|   `-- Pitbull
`-- Pop
    |-- Bruno_Mars
    `-- Justine_Bieber

Conclusion

In Linux system mkdir is used to create new directories or folders. We have gone through the mkdir command with some options. You can always use its manual and read further about the command.

Practice this command as much as you can. System admins use it widely and frequently. There is not much to look into this command. It comes under basic commands in Linux.

For the complete detailed information about mkdir command, visit man mkdir or mkdir --help in the Linux terminal.

Let us know if you have queries while following this blog in the comments.