TExcelTAD
Home › Linux & Bash › mkdir & rmdir

mkdir & rmdir

mkdir (make directory) creates directories and rmdir (remove directory) deletes empty ones. Together with -p they let you create or tidy up whole folder structures in one command.

Creating directories

bash · try it yourself
mkdir photos
mkdir music videos
ls -F
music/
photos/
videos/

Each argument becomes a new directory. Trying to create one that already exists, or one inside a parent that doesn't exist, fails:

bash · try it yourself
mkdir photos
mkdir photos
mkdir 2026/january
mkdir: cannot create directory ‘photos’: File exists
mkdir: cannot create directory ‘2026/january’: No such file or directory

-p: parents and no errors

mkdir -p creates every missing parent directory along the path and stays quiet if the directory already exists. That makes it the safe choice for scripts, which often run more than once:

bash · try it yourself
mkdir -p 2026/january/receipts
mkdir -p 2026/january/receipts
echo "exit status: $?"
find 2026
exit status: 0
2026
2026/january
2026/january/receipts

Creating many directories at once

Combine -p with brace expansion ({a,b,c} and {1..3}) to build a whole tree in one line. Bash expands the braces into separate arguments before mkdir runs:

bash · try it yourself
mkdir -p webapp/{src/{components,pages},public,tests}
mkdir -p logs/2026-{01..03}
echo webapp/{src,tests}
find webapp logs | sort
webapp/src webapp/tests
logs
logs/2026-01
logs/2026-02
logs/2026-03
webapp
webapp/public
webapp/src
webapp/src/components
webapp/src/pages
webapp/tests

-v (verbose) makes mkdir report each directory it creates, which is handy in setup scripts:

bash · try it yourself
mkdir -pv data/{raw,clean}
mkdir: created directory 'data'
mkdir: created directory 'data/raw'
mkdir: created directory 'data/clean'

Tip: mkdir -m 700 secrets creates a directory with specific permissions (here: only you can enter it). Permissions are covered in the chmod lesson.

Removing empty directories

rmdir only deletes directories that are empty. That is a safety feature: you can't lose files with it.

bash · try it yourself
mkdir -p old/empty-folder
touch old/keep.txt
rmdir old/empty-folder
rmdir old
ls old
rmdir: failed to remove 'old': Directory not empty
keep.txt

The second rmdir refuses because old still contains keep.txt. To delete a directory and everything inside it you need rm -r (next lesson), which deserves much more care.

rmdir -p

rmdir -p a/b/c removes c, then b, then a, stopping as soon as one of them is not empty:

bash · try it yourself
mkdir -p archive/2025/q4
rmdir -pv archive/2025/q4
ls
rmdir: removing directory, 'archive/2025/q4'
rmdir: removing directory, 'archive/2025'
rmdir: removing directory, 'archive'

Naming tips

Do Avoid
monthly-reports, monthly_reports Monthly Reports (spaces need quoting everywhere)
lowercase names mixing Docs and docs (two different directories on Linux)
ISO dates 2026-03 03-2026 (doesn't sort chronologically)

Test yourself

Which command creates a/b/c even if a doesn't exist yet?

  1. mkdir a/b/c
  2. mkdir -p a/b/c
  3. mkdir -r a/b/c
  4. rmdir -p a/b/c

What happens when you run rmdir on a directory that contains a file?

  1. The directory and file are deleted
  2. Only the file is deleted
  3. It fails with Directory not empty
  4. It asks for confirmation

What does mkdir -p site/{css,js} create?

  1. A directory literally named {css,js}
  2. site/css and site/js
  3. Only site
  4. css and js in the current directory