Linux mkdir command

Updated: 11/04/2017 by Computer Hope

About mkdir

The mkdir command creates new directories in your file system.

Syntax

mkdir [-m=mode] [-p] [-v] [-Z=context] directory [directory ...]
mkdir --version
mkdir --help

Options

directory The name of the directory to be created. If the specified directory does not already exist, mkdir creates it. More than one directory may be specified.

A specified directory can include path information. For instance, if the current directory is /home/hope, and you'd like to create the directory /home/hope/Documents/writing, you can use the command mkdir Documents/writing. If the Documents folder does not already exist, you should specify the -p option to create it automatically, otherwise the command will fail.
-m=mode,
--mode=mode
You can use the -m option to set a file mode (permissions, etc.) for the created directories. The syntax of mode is the same as with the chmod command.
-p,
--parents
Create parent directories as necessary. When this option is specified, no error is reported if a directory already exists.
-v,
--verbose
Verbose output. Print a message for each created directory.
-Z=context,
--context=context
If you are using SELinux, this option sets the security context of each created directory to context. For detailed information about security contexts, consult your SELinux documentation.
--help Display a help message, and exit.
--version Display version information, and exit.

Exit status

mkdir returns an exit status of zero if all operations were successful, or a non-zero exit status if operations failed.

Examples

mkdir myfiles

Create a new directory called myfiles in the current directory.

mkdir ~/myfiles

Create the directory myfiles in your home directory, specified here with a tilde ("~"). For more information about using ~ to represent your home directory, see tilde expansion in bash.

mkdir -m a=rwx mydir

Create the mydir directory, and set its file mode (-m) so that all users (a) may read (r), write (w), and execute (x) it.

For directories, this means that any user on the system may view ("read"), and create/modify/delete ("write") files in the directory. Any user may also change to ("execute") the directory, for example with the cd command.

chdir -m 777 mydir

Same as the above command, but using a numerical file mode. Grants read, write, and execute permissions to the directory for all users. (For more information about file modes, see chmod).

mkdir -p /home/hope/Documents/pdf

Creates the directory /home/hope/Documents/pdf. If any of the parent directories /home, /home/hope, or /home/hope/Documents do not already exist, they will automatically be created.

rmdir — Remove a directory.