How to Use the mkdir Command in Linux

Short for “make directory”. As the name implies, this command is used to create a new directory when it does not exist. It will warn you when creating a directory that already exists.

General syntax for mkdir command:

$ mkdir [OPTION...] [DIRECTORY...]
Display detailed information of command
mkdir -v

Use the -v (verbose) option to see what the mkdir command is doing in the background.

Example: Printing information about what the command does when creating the NameDir directory

show command info
Create multiple directories
mkdir [dir1] [dir2] [dir3]

Create multiple directories by specifying the directory names as command arguments, separated by spaces.

Example: Creating dir1, dir2 and dir3 directories under /Desktop/Command path.

create multiple dir
Set directory permissions
mkdir -m [permission] [dir]

Use the -m (-mode) option with the mkdir command to create a directory with specific permissions.

Example: Let’s create the DirName directory with read-only permission.

set directory permission
Create directory with SELinux context
mkdir -Z [dirname]

This option is used to set default SELinux rules for a specific directory at creation time.

Example: Creating the ExampleDir directory with SELinux context.

create dir with SELinux
Create multi-level directories
mkdir -p [directory_tree]

Use the -p option with the mkdir command to create a complete directory structure.

Example: Let’s create the directory tree dirname1/dirname2/dirname3, only works if none of these directories already exist.

create multi-level dir