How to Create Directories in Linux (mkdir Command)
Updated Dec 3, 2019
In Linux systems, you can create new directories either from the command line or with the help of your desktop’s file manager. The command that allows you to create directories (also known as folders) is mkdir .
This tutorial covers the basics of using the mkdir command, including everyday examples.
Linux mkdir Command Syntax #
The syntax for the mkdir command is as follows:
The command takes one or more directory names as its arguments.
How to Create a New Directory #
To create a directory in Linux pass the name of the directory as the argument to the mkdir command. For example, to create a new directory newdir you would run the following command:
You can verify that the directory was created by listing the contents using the ls command :
When providing only the directory name, without the full path, it is created in the current working directory.
The current working directory is the directory from which you are running the commands. To change the current working directory, use the cd command.
To create a directory in another location you’ll need to provide the absolute or relative file path to the parent directory. For example, to create a new directory in the /tmp directory you would type:
If you try to create a directory in a parent directory where the user does not have sufficient permissions you will receive Permission denied error:
The -v ( –verbose ) option tells mkdir to print a message for each created directory.
How to Create Parent Directories #
A parent directory is a directory that is above another directory in the directory tree. To create parent directories, use the -p option.
Let’s say you want to create a directory /home/linuxize/Music/Rock/Gothic :
If any of the parent directories don’t exist you will get an error as shown below:
Instead of creating the missing parent directories one by one, invoke the mkdir command with the -p option:
When the -p option is used, the command creates the directory only if it doesn’t exist.
If you try to create a directory that already exists and the -p option is not provided, mkdir will print File exists error:
How to Set Permissions when Creating a Directory #
To create a directory with specific permissions, use the -m ( -mode ) option. The syntax for assigning permissions is the same as with the chmod command.
In the following example, we’re creating a new directory with 700 permissions, which means that only the user who created the directory will be able to access it:
When the -m option is not used, the newly created directories usually have either 775 or 755 permissions, depending on the umask value.
How to Create Multiple Directories #
To create multiple directories, specify the directories’ names as the command arguments, separated by space:
The mkdir command also allows you to create a complex directory tree with one command:
The command above creates the following directory tree :
The mkdir command in Linux is used to create new directories.
For more information about mkdir , visit the mkdir man page .
If you have questions, feel free to leave a comment below.
How to Use ‘mkdir’ to Create Linux Directories
Make folders from a terminal with this simple command
What to Know
- To create directories in Linux, you can open Terminal and use the command line with the mkdir command.
- Once you create a new directory in Linux, then you can change permissions and create folders within the directory.
- You can also create a directory and set permissions at the same time.
You can create new directories in Linux using the command line. The command to create directories is mkdir. Here is how to create directories with mkdir.
This article also looks at the switches used with mkdir and the proper syntax required to make the command work correctly.
How to Create a New Directory
The simplest way to create a new directory is to enter the following command:
For example, to create a directory called test, open a terminal window, navigate to the folder where you want the new folder, then enter:
Change the Permissions of the New Directory
After creating a new folder, set the permissions so that only a certain user can access it or so that some people can edit files in the folder, but others have read-only permissions.
To continue with the example above, run the ls command to see the permissions for the folder called test:
Run the ls command in the folder where the test folder is located. If it’s your home directory (for example, you didn’t use the cd command), then you don’t have to change the directory here.
You should see something like this (but probably with several other entries, considering that there are other folders there):
The permissions are drwxrwxr-x, 2, owner, and group.
- The d indicates that test is a directory.
The first three letters following the d are the owner permissions for the directory specified by the owner’s name:
r is for read.
- w is for write.
- x is for execute (which means you can access the folder).
- The d indicates that test is a directory.
- The next three characters are the group permissions for the file specified by the group name. The options are r, w, and x. The hyphen means that a permission is missing. In the example above, anybody belonging to the group can access the folder and see the files, but can’t write to the folder.
- The final characters are the permissions that all users have, and these are the same as the group permissions.
To change the permissions for a file or folder, use the chmod command. The chmod command lets you specify three numbers which set the permissions:
Add the numbers together for a mixture of permissions. For example, to attribute read and execute permissions, the number is 5 (4+1), or for read and write permissions, the number is 6 (4+2).
You must specify three numbers as part of the chmod command. The first number is for the owner permissions, the second is for the group permissions, and the last is for everyone else.
For example, for the owner to have full permissions, the group to have read and execute permissions, and anyone else to have no permissions, enter the following:
Use the chgrp command to change the group name that owns a folder. For example, to create a directory that the accountants in a company can access, first make the group accounts by typing the following:
If you don’t have the correct permission to create a group, use sudo to gain extra privileges or use the su command to switch to an account with valid permissions.
Next, change the group for a folder by typing the following:
To give the owner and everybody else in the accounts group read, write, and execute, but read-only access to others, use the following command:
How to Create a Directory and Set Permissions at the Same Time
You can create a directory and set the permissions for that directory at the same time using the following command:
This command creates a folder that everybody can access. It’s rare to create folders with this kind of permission.
Create a Folder and Any Parents That Are Required
You can create a directory structure without creating each individual folder, for example, to create folders for music as follows:
It takes time to create the rock folder for alice cooper and queen, followed by the rap and jazz folders for the others.
By specifying -p, you can create all the parent folders on the fly if these don’t exist:
For example, this mkdir command makes one of the folders listed above:
Get Confirmation That a Directory Was Created
By default, the mkdir command doesn’t tell you if the directory was created successfully. Usually, if no errors are shown, you can assume it worked. However, if you want more verbose output so that you know what’s been created, use the -v switch:
How to Create Directory in Linux using mkdir Command
Let’s check how to create a directory in the Linux system. On Linux, we can use ‘mkdir’ command. Mkdir is short for “make directory”. All most all Linux distribution like Ubuntu, RHEL, Debian and Centos uses the same command.
In this tutorial, I will show you 5 usage mkdir command to create directory on a Linux and Unix like operating system.
1) mkdir command
You can type mkdir directly from your console to use it.
By default, running mkdir without any parameter will create a directory under the current directory. Here’s a sample of it:
From the screenshot above, we created a directory called ‘office’. When we run mkdir command, we are in ‘/home/pungki’ directory. So then the new directory, which is office, is created under /home/pungki directory. If we put an exact location – for example : ‘/usr/local’ – , then Linux will create a directory under ‘/usr/local’ directory.
When Linux found that the directory which suppose to be created already exists, then the tool will tell us that it can’t create it.
Another pre-requisite for creating a directory that you must have access to the location where the directory wants to be created. When you don’t have it then mkdir will report an error.
2) Create multiple directories
We can also create multiple directories at the same time. Let say we want to create directories named ‘ubuntu’, ‘redhat’ and ‘slackware’. Then the syntax will be like this :
3) Add directory include its sub-directory
When you want to create sub-directories , you will need to use -p parameter. This parameter will create parent directory first, if mkdir cannot find it. Let say we want to create directory named ‘letter’ and directory named ‘important’ under directory letter. Then the syntax will be like this:
4) Set access privilege
Using -m parameter, we can also set the access privilege for the new directory on-the-fly. Here’s an example.
The above command will create a directory named letter and give access privilege read-only for the directory owner, directory group owner and anybody.
5) Print message for each created directory
The -v option will print message on console for each created directory. Check the following example.
6) Create Directory with Date
Do you know that you can create a directory name with date? This is mostly useful in shell scripts when you require to create backup folders with date.
Mkdir command is one of the basic commands used in in Linux. As usual, you can always type man mkdir or mkdir –help to display manual page and explore it more detail.