How to create Directory in Linux (mkdir Command)
In Linux if you want to create a new directory, you can do so by using the
mkdir command. The
mkdir command is used to make a directory in Linux.
Before jumping into how we can create a directory(or in Windows terminology - a folder), first let's see how the
mkdir command works.
Here is the syntax for the
mkdir [OPTION] [DIRECTORY]
We can provide some options and the path of one or more directories while using the
To see the available options, you can run the following command in yout terminal:
Now let's see how we can create a directory using the
mkdir command in Linux.
Creating a Directory
We will start by creating a single directory using the
The above command will create a new directory with name studytonight in the present working directory. You can use the ls command to list down the files and directories in the current directory.
drwxrwxr-x 2 nobody nobody 6 Jul 2 17:29 studytonight
By default the directory is created inside the current directory. If we want to create the directory in some other location, then we can provide the complete path while creating the directory.
Let's take an example for it. Consider that we have a directory structure:
And we are currently inside the directory someuser and want to create a new directory with name sample inside the directory test-linux, we can do so using the following command:
Let's create one more directory. Consider that you are currently inside the directory test-linux and you want to create a directory inside the directory learn-linux with name sample, we can do using the following command:
The two dots in the beginning represents that we want to move out of the test-linux directory, then go inside the learn-linux directory and create a directory named sample there.
Note: While creating a directory you may get Permission Denied error if you do not access to any location and you try to create a directory there.
Creating a directory along with Parent Directories
If we want to create a directory structure for example in the above given directory structure, if you are currently inside the someuser directory and you want to create a directory sample inside the directory practice-linux, how will you do so? Notice, we do not have the practice-linux directory inside someuser directory.
We can use the mkdir command to create the parent directory too while creating a directory using the
Let's see how this works,
mkdir -p practice-linux/sample
When we run the above command, a new directory practice-linux will be created and inside it a sample directory will also get created. This is the magic of the
-p option of the
Similarly, you can use the -p option to create multiple level of parent directories.
Creating Multiple Directories
If we want to create multiple directories in one go, we can easily do so using the
mkdir dir1 dir2 dir3
The above command will create 3 directories in the current directory.
drwxrwxr-x 2 nobody nobody 6 Jul 2 17:50 dir1
drwxrwxr-x 2 nobody nobody 6 Jul 2 17:50 dir2
drwxrwxr-x 2 nobody nobody 6 Jul 2 17:50 dir3
See how simple it is. Similarly, you can provide different paths to create multiple directories all at different locations by running a single command.
Creating Directory with custom Permissions
When we use the mkdir command to create a directory in Linux, the default mode/permissions assigned to it is 755, which means read and execute permission for all, but not everyone can write to this directory. If you want to provide custom permissions like 700, or 644, or even more restrictive 444, we can do so using the
-m option(stands for mode) with the
Here's an example,
mkdir -m 444 dir1
dr--r--r-- 2 nobody nobody 6 Jul 3 10:31 dir1
This will create a new directory with 444 mode/permission, which you can see in the beginning of the output dr--r--r-- which represents the permissions. We can change the permission for any directory or file, using the chmod command in Linux.
In this tutorial we covered how we can create a directory or create multiple directories in Linux. We also learned how we can create parent directories along with creating a directory if the parent directories are not already present.