The mkdir command is used to create directories and folders in Linux operating system. The mkdir name comes from the “make directory” which can be also expressed as “make dir” where the “dir” is generally used for directory and there is a command with the same name. Also, the mkdir command can be used to set permission and create multiple directories. The mkdir is an essential tool provided by all Linux distributions like Ubuntu, Debian, Mint, Kali, Fedora, CentOS, RHEL by default.
mkdir Command Syntax
The mkdir command has the following syntax where options and parameters are provided.
mkdir OPTIONS DIR_NAME
- OPTIONS are used to provide different options or parameters to the mkdir command. OPTIONS are optional.
- DIR_NAME is the name of directory or folder we want to create. Multiple directory names can be provided by separating them. DIR_NAME is required.
Print mkdir Command Help Information and Parameters
Before starting to use the mkdir command the help information and parameters can be listed with the –help option.
mkdir --help
Usage: mkdir [OPTION]… DIRECTORY… Create the DIRECTORY(ies), if they do not already exist. Mandatory arguments to long options are mandatory for short options too. -m, --mode=MODE set file mode (as in chmod), not a=rwx - umask -p, --parents no error if existing, make parent directories as needed -v, --verbose print a message for each created directory -Z set 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 GNU coreutils online help: https://www.gnu.org/software/coreutils/ Full documentation https://www.gnu.org/software/coreutils/mkdir or available locally via: info '(coreutils) mkdir invocation'
From the output, we can see that the mkdir command is not so complex it provides only 7 parameters or options.
Print mkdir Command Version
Even it is not important as a stable and not regularly changing command the version of the mkdir command can be printed with the –version option like below.
mkdir --version
Create New Directory
Lets start action where we wil create a directory by using the mkdir command. As expected this directory will be empty and we will provide the name of the directory. The standard way to create a directory from command line or terminal is the mkdir command. In the following example we will create a directory named “data”.
mkdir data
If the directory is created without problem there will be no output. We can check the newly created directory with the ls command like below.
ls -l
If there is an error the error printed into the console. For example, if the provided directory already exists we will get the following error.
mkdir: cannot create directory ‘data’: File exists
Create Multiple Directories
Creating multiple directories with a single mkdir command is very powerful feature of it. The new directry names are provided as parameters into the mkdir command. They are separated with spaces.
mkdir data1 data2 data3
If the directory names contains spaces we should use double qutos in order to specify the directoy names and prevent space related errors.
mkdir "data 1" "data 2" "data 3"
Create Multilevel Directories or Non-existing Parents
By default multilevel directories can not be created with the mkdir command. For example, if the directory named year does not exist the directory named 2020 can not be created. If you try to create it you will get the following error.
mkdir year/2020
mkdir: cannot create directory ‘year/2020’: No such file or directory
But the -p option can be used to create the child directories as well as the parent directories even both of them do not exist. The following command will create the year and 2020 directories without any error.
mkdir -p year/2020
Alternatively we can specify the full or absolute path of the multilevel directories which will be more stable.
mkdir -p /mnt/year/2020
Create Multilevel and Multiple Directories
A popular use case for the mkdir command is creating non existing parent with multiple childs in a single command by providing them as a single expressions. In the following example we will create the parent directory year and then create multiple child directories named 2019, 2020, 2021.
mkdir -p /mnt/year/{2019,2020,2021}
Set Permissions When Creating Directory
Every Linux directory or folder has permissions. These permissions are assigned automatically by using default values which are specified with the umask command. But we can specify the permissions of the newly created directory by using the -m option. In the following example, we will set the newly created directory permission as 777 by using mkdir command.
mkdir -m777 data
Set SELinux Security Context When Creating Directory
SELinux is a security mechanism used in Linux systems. SELinux is similar to the permissions but with more granular configuration. The SELinux is provided as user:role:type information for the newly created directory. The –context is used to specify the security context.
mkdir --context=system_u:object_r:httpd_sys_content_t /var/www/website
Print Verbose Information During Directory Creation
The directory creation can be listed in a verbose mode by using the -v option. This will print information for every directory creation.
mkdir -v data1 data2 data3 data4 data5
mkdir: created directory 'data1' mkdir: created directory 'data2' mkdir: cannot create directory ‘data3’: File exists mkdir: created directory 'data4' mkdir: created directory 'data5'
We can see that the successfully created directories are expressed as “mkdir: created directory ‘data1’” but if there is a problem or error like the directory is all ready exist the “mkdir: cannot create directory ‘data3’: File exists” error is printed.