In UNIX/Linux, the mkdir command is used to create single or multiple directories (also referred to as folders in some operating systems) in the current working directory.
If the specified directory is already present in the current working directory, then the “File exists
” error will be thrown unless the “-p
” flag is not assigned.
Apart from that, you can also specify the permission in character or numeric while creating the directory, but to perform all this action, you should have proper permissions.
In this article, you will learn all about the mkdir command with practical examples.
Tutorial Details
Description | Mkdir |
Difficulty Level | Low |
Root or Sudo Privileges | No |
OS Compatibility | Ubuntu, Manjaro, Fedora, etc. |
Prerequisites | mkdir |
Internet Required | No |
Syntax of the Mkdir Command
The mkdir command takes two arguments: one is the option, and the other is the directory name.
$ mkdir [OPTION] [DIRECTORY]
The following is the list of known options:
Options | Description |
---|---|
"-m", "--mode=MODE" | Set permissions while creating a new directory (like the chmod command). |
"-p", "--parents" | It will create a parent directory and will not throw an error if the specified directory is already present. |
"-v", "--verbose" | Print the message for each directory created. |
"-Z" | Set the SELinux security context of each directory to the default type. |
"--context[=CTX]" | Indentical to “-Z “, or if CTX is specified, then set the SELinux or SMACK security context to CTX. |
"--help" | Display the help section. |
"--version" | Display the binary version. |
Creating a New Directory
The following command, without any flag, will create a single directory with the specified name in your current working directory.
$ mkdir mydir
Output:
Creating Multiple Directories
Instead of one, specify multiple directories separated by a space to create them in your current working directory.
$ mkdir mydir1 mydir2 mydir3
Output:
Creating Multiple Directories Using Special Characters
Unlike giving a space, you can specify multiple directories within the “{}
” curly bracket, separated with a “,
” comma, to create them in your current working directory.
$ mkdir {mydir1,mydir2,mydir3}
Output:
Creating a New Directory in the Desired Location
Define the absolute or relative path to create a new directory in your desired location (restricted locations require root or sudo privileges).
$ mkdir ~/Documents/newdir
The above command will create a new directory with the name “newdir
” in the “~/Documents
” directory.
Creating a New Directory in Two Different Desired Locations
Same as previous command, specify the complete path of two desired locations with the new directory name separated with a space.
$ mkdir ~/Documents/newdir ~/Downloads/newdir
Output:
As you can see, the “newdir
” directory is created inside both the “~/Documents
” and “~/Downloads
” directories.
Creating Multiple Directories Using Special Characters at Your Desired Location
Again, specify the complete path with multiple directories’ names inside the “{}
” curly bracket with a “,
” comma as a separator.
$ mkdir ~/Documents/{mydir1,mydir2,mydir3}
Output:
Ignoring the Error if the Specified Directory is Already Present
If you are creating a new directory and a directory with the same name already exists, then you will get the “File exists
” error, as shown.
$ mkdir newdir
$ mkdir newdir
Output:
In this scenario, you can use the “-p
” flag; it will create a new directory only if the directory is not present in your current working directory; otherwise, it will exit the command without throwing any errors.
$ mkdir -p newdir
$ mkdir -p newdir
Output:
Creating a Parent Directory While Creating a New Directory
When you create a new directory inside another directory, if the parent directory does not exist, you will get the “No such file or directory
” error.
$ mkdir dir1/dir2/dir3
Output:
As you can see above, the “dir1
” directory, including the “dir2
” directory, is not present, which is why you got an error.
To avoid this error, simply use the “-p
” flag to create a new directory that includes all parent directories.
$ mkdir -p dir1/dir2/dir3
Output:
Creating a Sub-Directory Inside the Newly Created Multiple Directories
The following command will create a new sub-directory with the name “data
” inside all three directories “mydir1
“, “mydir2
“, “mydir3
“, as shown.
$ mkdir -p ~/Documents/{mydir1,mydir2,mydir3}/data
Output:
Printing the Directory Name While They Are Created
Use the “-v
” flag to print each directory name while it is being created in the output.
$ mkdir -v -p ~/Documents/{mydir1,mydir2,mydir3}/data
Output:
Setting Permissions While Creating a New Directory
Use the “-m
” flag to set the file mode (or permission) while they are created, as shown.
$ mkdir -m g=rwx ~/Documents/newdir
The above command will grant the directory owner and group of users complete permission.
Instead of attributes, you can also specify the permissions in numeric values.
$ mkdir -m 777 ~/Documents/newdir
Output:
This way, you can easily manage to give separate permissions for the owner, a group of users, and others.
And that was all you should know about this command.
If you have any question or query regarding this topic, feel free to ask it in the comment section.
Join The Conversation
Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.