Linux Command Reference ยท Files and Directories
Linux mkdir command
Reference for the Linux mkdir command: create directories, nested paths with -p, set permissions with -m, brace expansion and common errors.
mkdir creates one or more directories. By default every parent of the new directory must already exist; the -p option creates missing parents too and, as a bonus, does not complain when the directory is already there, which makes it ideal for scripts that must be safe to run twice.
New directories get the mode 0777 minus your umask (typically 0755 or 0775). The -m option sets the mode directly.
mkdir [OPTION]... DIRECTORY...
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
mkdir [-pv] [-m MODE] DIRECTORY...
| Option | Description |
|---|---|
-p, --parents |
Create missing parent directories; no error if the directory already exists. |
-m MODE, --mode=MODE |
Set permissions of the new directory, in octal (750) or symbolic (u=rwx,go=rx) form. |
-v, --verbose |
Print a message for each directory created. |
-Z |
Set the SELinux security context to the default type (where supported). |
Returns: 0 if all directories were created; non-zero if any failed. With several arguments, mkdir keeps going and reports each failure.
Common errors: "File exists" (a file or directory of that name is already there), "No such file or directory" (a parent is missing and -p was not used), "Permission denied" (no write permission on the parent), "Not a directory" (a component of the path is a regular file).
Usage notes
-pis idempotent, but only if the existing item is a directory. If a regular file has that name,mkdir -pstill fails.-mand parents: with-p, the mode applies only to the final directory. Parents created on the way get the default mode (and always have owner write and execute added, so the command can continue).umaskinteraction: without-m, the umask removes bits from0777. With-m, the mode is applied as given, except that a symbolic mode likeg+wis relative to the default.- Brace expansion is done by the shell before
mkdirruns:mkdir -p logs/{2024,2025}/{01..12}creates twenty-four directories in one command. Remember the braces only work in Bash, Zsh and Ksh, not in plainsh. - Directory names with spaces need quotes:
mkdir "My Documents". - Names starting with a dash need
--:mkdir -- -odd. - Temporary directories: use
mktemp -dinstead of inventing a name in/tmp; it avoids collisions and races. - Setting an owner needs another command (
chown) orinstall -d -o user -g group -m 755 dir. - Race-free creation:
mkdirwithout-pis atomic and fails if the name exists, which scripts sometimes use as a simple lock.
Related
rmdir,touch,ls,rm- Lessons: Creating files and directories, File permissions