Linux systems give you access to create new directories. You can do this either from the command line or with the help of your desktop’s file manager. The command enables you to create directories which are (also referred to as folders) is mkdir.
What is the mkdir Command in Linux?
The mkdir command in Linux/Unix enables users to create or make new directories. Understand that mkdir stands for “make directory”.
With mkdir, you can also set permissions, create multiple directories (folders) at once, and much more.
Linux mkdir Command Syntax
The syntax for the mkdir command is as follows:
mkdir (OPTION) [DIRECTORY]
The command takes one or more directory names as its arguments.
How to Create a New Directory in Linux
To easily create a new directory in Linux, pass the name of the directory as the argument to the mkdir command. For instance, to create a new directory newdir, you would have to run the following command:
- $ mkdir newdir
You can verify that the directory was created by listing the contents using the Is command.
- $ Is – 1
- Output
drwxrwxr – c 2 username username 4096 Jan 20 03:39 newdir
When you are providing only the directory name, without the full path, it is created in the current working directory.
The current working directory happens to be the directory from which you are running the commands. In order to change the current working directory, you have to use the cd command.
If you want to create a directory in another location, you’ll need to provide the absolute or relative file path to the parent directory.
For instance, in order to create a new directory in the /tmp directory you would type:
- $ mkdir /root/newdir
- Output
mkdir: cannot create directory ‘/root/newdir’ : Permission denied
- The –v (–verbose) option prompts mkdir to print a message for each created directory.
How to Create Parent Directories
A parent directory is a directory which is above another directory in the directory tree. In order to create parent directories, you have to use the –p option.
If for instance, you want to create a directory /home /linuxize /Music /Rock /Gothic:
$ mkdir /home/linuxize /Music /Rock /Gothic
If any of the parent directories do not exist, you will get an error:
Output
mkdir: cannot create directory ‘/home /linuxize /Music /Rock/Gothic’: No such file or
Rather than creating the missing parent directories one after the other, invoke the mkdir command with the -p option:
$ mkdir -p /home/linuxize /Music /Rock /Gothic
If the -p option is being used, the command creates the directory only if it does not exist.
If you try to create a directory which already exists and the -p option is not provided, mkdir prints File exists error.
$ mkdir newdir
Output
mkdir: cannot create directory ‘newdir’: File exists.
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 this example we will be creating a new directory with 700 permissions. This implies that only the user who created the directory will be able to access it:
$ mkdir -m 700 newdir
When the -m option is not used, then the newly created directories normally have either 775 or 755 permissions, based on the umask value.
Creating Multiple Directories
To successfully create multiple directories, you have to specify the directories’ names as the command arguments, separated by space.
$ mkdir dirl dir2 dir3
The mkdir command also enables you to create a complex directory tree with one command:
$ mkdir -p Music/{jazz/Blues, Folk, Disco, Rock /(Gothic, Punk, Progressive), Classical /Ba
Now the command above creates the following directory tree:
Output
Music/
|- – Classical
| –
- – Baroque
- -Early
| – – Disco
| – – Folk
| – – Jazz
| –
- – Blues
–Rock
| – – Gothic
|– Progressive
–Punk
On the whole, the mkdir command in Linux is used in creating new directories.