# mkdir — Create Directories

> Create directories — single dirs, nested trees and permissions set at creation time. One of the most basic, most frequently used Unix commands.

Source: https://www.jpkc.com/db/en/cheatsheets/files-text/mkdir/

<!-- PROSE:intro -->
mkdir creates directories – from a single folder through nested trees to complete project structures in a single call. The key to productivity is the `-p` option: it creates missing parent directories along the way and reports no error if the directory already exists – ideal for scripts. With `-m` you set permissions right at creation time, and with brace expansion you build whole directory matrices in one command. This guide covers the basics, permissions, brace patterns, and common project layouts.
<!-- PROSE:intro:end -->

## Basic Usage

`mkdir <directory>` — Create a single directory.

```bash
mkdir projects
```

`mkdir <dir1> <dir2> <dir3>` — Create multiple directories at once.

```bash
mkdir css js images
```

`mkdir -v <directory>` — Verbose mode. Print a message for each created directory.

```bash
mkdir -v logs backups tmp
```

## Parent Directories

`mkdir -p <path/to/directory>` — Create parent directories as needed. No error if the directory already exists.

```bash
mkdir -p /var/www/html/assets/css
```

`mkdir -p <dir1>/sub1 <dir2>/sub2` — Create multiple nested directory trees at once.

```bash
mkdir -p src/components src/utils src/hooks
```

`mkdir -pv <path/to/directory>` — Create parent directories with verbose output showing each directory created.

```bash
mkdir -pv /opt/app/config/ssl
```

## Permissions

`mkdir -m <mode> <directory>` — Create a directory with specific permissions (octal mode).

```bash
mkdir -m 755 /var/www/html
```

`mkdir -m 700 <directory>` — Create a private directory. Only the owner can access it.

```bash
mkdir -m 700 ~/.ssh
```

`mkdir -m 1777 <directory>` — Create a directory with sticky bit. Users can only delete their own files.

```bash
mkdir -m 1777 /tmp/shared
```

`mkdir -m 2775 <directory>` — Create a directory with setgid. New files inherit the directory's group.

```bash
mkdir -m 2775 /var/www/shared
```

`mkdir -pm <mode> <path/to/directory>` — Create nested directories with specific permissions on the final directory.

```bash
mkdir -pm 700 ~/.config/app/secrets
```

## Brace Expansion Patterns

`mkdir -p <base>/{<dir1>,<dir2>,<dir3>}` — Create multiple subdirectories using brace expansion.

```bash
mkdir -p project/{src,tests,docs}
```

`mkdir -p <base>/{<dir1>,<dir2>}/{<sub1>,<sub2>}` — Create a nested directory matrix using nested brace expansion.

```bash
mkdir -p app/{frontend,backend}/{src,tests,config}
```

`mkdir -p <base>/{01..12}` — Create numbered directories using sequence expansion.

```bash
mkdir -p archive/2024/{01..12}
```

`mkdir -p <base>/{a..z}` — Create alphabetically named directories using character ranges.

```bash
mkdir -p index/{a..z}
```

`mkdir -p project/{src/{components,utils,hooks},public/{css,js,images},tests}` — Create a complete project directory structure in one command.

```bash
mkdir -p project/{src/{components,utils,hooks},public/{css,js,images},tests}
```

## Common Project Structures

`mkdir -p <project>/{src,dist,tests,docs}` — Basic project scaffold with source, build output, tests, and docs.

```bash
mkdir -p my-app/{src,dist,tests,docs}
```

`mkdir -p <project>/{src/{css,js,images},dist,node_modules}` — Frontend project structure with asset directories.

```bash
mkdir -p website/{src/{css,js,images},dist,node_modules}
```

`mkdir -p <project>/{app/{Controllers,Models,Views},config,public,storage/{logs,cache}}` — MVC web application structure.

```bash
mkdir -p my-app/{app/{Controllers,Models,Views},config,public,storage/{logs,cache}}
```

`mkdir -p <project>/{cmd,internal,pkg,api,configs,scripts,test}` — Go project standard layout.

```bash
mkdir -p my-service/{cmd,internal,pkg,api,configs,scripts,test}
```

## Temporary Directories

`mktemp -d` — Create a temporary directory with a unique name in /tmp. Returns the path.

```bash
TMPDIR=$(mktemp -d); echo $TMPDIR
```

`mktemp -d -t <template>` — Create a temporary directory with a custom name prefix.

```bash
mktemp -d -t myapp-XXXXXX
```

`mktemp -d -p <directory>` — Create a temporary directory inside a specific parent directory.

```bash
mktemp -d -p /var/tmp
```

`mktemp -d && trap 'rm -rf "$TMPDIR"' EXIT` — Create a temp directory that is automatically cleaned up when the script exits.

```bash
TMPDIR=$(mktemp -d) && trap 'rm -rf "$TMPDIR"' EXIT
```

## Common Patterns

`mkdir -p <dir> && cd <dir>` — Create a directory and immediately enter it.

```bash
mkdir -p ~/projects/new-app && cd ~/projects/new-app
```

`[ -d <dir> ] || mkdir -p <dir>` — Create a directory only if it does not exist (explicit check).

```bash
[ -d /var/log/app ] || mkdir -p /var/log/app
```

`install -d -m <mode> -o <owner> -g <group> <directory>` — Create a directory with permissions, owner, and group in one command.

```bash
install -d -m 755 -o www-data -g www-data /var/www/html
```

`mkdir -p <dir> && cp <files> <dir>/` — Create destination directory and copy files into it.

```bash
mkdir -p /backup/2024 && cp *.sql /backup/2024/
```

`for d in <list>; do mkdir -p "$d"; done` — Create directories from a dynamic list or variable.

```bash
for d in dev staging prod; do mkdir -p "/deploy/$d/releases"; done
```

<!-- PROSE:outro -->
## Conclusion

mkdir is one of the most basic commands there is, but two options make it far more convenient: `-p` creates missing parent directories and stays silent when the directory already exists (perfect for idempotent scripts), and `-m` sets permissions at creation time, avoiding a sensitive race condition between mkdir and a following chmod. Combined with brace expansion, you can scaffold entire project trees in a single command. For throwaway work directories, reach for `mktemp -d` instead of predictable names in /tmp.

## Further Reading

- [GNU coreutils: mkdir invocation](https://www.gnu.org/software/coreutils/manual/html_node/mkdir-invocation.html) – official reference for every option
- [Linux man page: mkdir(1)](https://man7.org/linux/man-pages/man1/mkdir.1.html) – the canonical manual page
<!-- PROSE:outro:end -->

## Related Commands

- [touch](https://www.jpkc.com/db/en/cheatsheets/files-text/touch/) – create empty files and set timestamps
- [rm](https://www.jpkc.com/db/en/cheatsheets/files-text/rm/) – remove files and directories
- [ls](https://www.jpkc.com/db/en/cheatsheets/files-text/ls/) – list directory contents

