The mkdir command in Linux is used to make directories, hence the command is named mkdir. With traditional GUI systems, if you want multiple directories, you have to create one directory at a time. The mkdir is a very flexible and customizable command in this matter. Let’s look at the functionality.
Using the mkdir command in Linux
The mkdir command can be used without any options to create a directory or even multiple directories with a single line.
1. Creating single and multiple directories with mkdir
root@HowLinux:~# ls root@HowLinux:~# mkdir one root@HowLinux:~# ls one ------- For multiple directories ----- root@HowLinux:~# mkdir one two three four root@HowLinux:~# ls four one three two
2. Creating parent directories automatically if they don’t exist
When creating directories, you might want to create a folder hierarchy structure. But creating that one folder at a time is not very convenient. That’s where the -p command comes to rescue. If you want to create a directory child3, inside the child2 folder, inside the child folder, inside a parent folder, with mkdir command, you’ll have to create them individually!
But with the addition of -p look how easy, it becomes.
root@HowLinux:~# mkdir parent/child/child2/child3 mkdir: cannot create directory ‘parent/child/child2/child3’: No such file or directory -------- In the above case, the mkdir command fails because the "parent" directory does not exist. ------ root@HowLinux:~# mkdir -p parent/child/child2/child3 root@HowLinux:~# ls parent
3. Print a message when a directory is created (useful when creating multiple directories)
In the above examples, you must have noticed that the mkdir command does not tell you if a folder was successfully created. Sure it does mention when it fails to create one. What if you want the command to print out a message every time a folder is created? The -v option gives you the results that you are looking for.
root@HowLinux:~# mkdir -v -p /parent/child/child2/child3 mkdir: created directory '/parent' mkdir: created directory '/parent/child' mkdir: created directory '/parent/child/child2' mkdir: created directory '/parent/child/child2/child3'
4. Creating a directory with specific permissions
By default, a directory created by mkdir has the write permissions for the user who created the folder. If you’d like to allow write permissions for other users or groups too while creating the directory, you can specify the permissions when creating the folder.
root@tryit-inviting:~# mkdir -m 0777 parent2 root@tryit-inviting:~# ls -l total 0 drwxr-xr-x 1 root root 10 Dec 12 06:34 parent drwxrwxrwx 1 root root 0 Dec 12 06:36 parent2
As you can see, the “parent” folder that we created previously has the drwxr-xr-x permission but the parent2 folder has drwxrwxrwx which means that all the users can write to this folder.
These are a few other less frequently used options which you can look into for further learning:
- -Z set the SELinux security context of each created directory to the default type
- –context[=CTX] like -Z, or if CTX is specified then set the SELinux or SMACK security context to CTX
- –help display this help and exit
- –version output version information and exit
The mkdir is a flexible, and a very useful command that you will tend to use regularly. So do keep trying it so you get used to the command and learn to use it efficiently.