mkdir: create directories
mkdir creates directories. With -p it creates a whole path in one go and does not complain if the directory already exists, which makes it safe in scripts.
mkdir [options] directory ...
Common options
| Option | Meaning |
|---|---|
-p | Create parent directories as needed; no error if it exists |
-m mode | Set the permissions of the new directory |
-v | Print each directory created |
Examples
mkdir projects # one directory
mkdir docs images scripts # several at once
mkdir -p /srv/app/releases/2026-11-02 # the whole path
mkdir -m 700 ~/private # only you can enter it
mkdir -p site/{css,js,img} # brace expansion (bash, zsh): three directories
mkdir -p logs/{2025,2026}/{01..12} # 24 directories for two years of months
Temporary directories
For scratch space in scripts, use mktemp -d instead of a fixed name in /tmp. It creates a new, private directory with a random name:
work=$(mktemp -d)
trap 'rm -rf "$work"' EXIT
echo "working in $work"
Things to know
- The permissions of a new directory are 777 minus your
umask, usually 755.-moverrides that for the last directory only; parents created by-pget the default. - To remove an empty directory, use
rmdir; for a non-empty one, seerm. - To create a directory owned by another user, create it as root and change the owner with
chown, or useinstall -d -o user -g group -m 750 /pathto do everything in one step.