opscape

Files and directories

rmdir

Remove empty directories, and refuse if they are not empty.

rmdir [OPTION...] DIRECTORY...

7 of 7 examples run in Debian 13.7 on 2026-10-31 · coreutils 9.7-3 · manual: rmdir(1)

rmdir deletes directories, but only empty ones. That restriction is the point: it can never take files with it, so it is the safe choice for tidying up after a script or removing a directory you believe is empty. If it is not, you get an error instead of data loss.

To delete a directory with its contents, use rm -r (see rm). To remove every empty directory below a point, use find . -type d -empty -delete (see find).

Examples

Remove an empty directory

Setup run before this example
mkdir old
rmdir old
ls -d old
outputexit 2
ls: cannot access 'old': No such file or directory

rmdir prints nothing on success; the ls afterwards fails because old is gone.

rmdir refuses a directory that has files in it

rmdir logs
explain
outputexit 1
rmdir: failed to remove 'logs': Directory not empty

logs contains three files, so rmdir leaves it alone and exits with 1. This is the safety net that rm -r does not have.

rmdir on a file

rmdir notes.txt
explain
outputexit 1
rmdir: failed to remove 'notes.txt': Not a directory

rmdir only removes directories. Use rm for files.

A directory that looks empty but is not

Setup run before this example
mkdir cache && touch cache/.gitkeep
ls cache
rmdir cache
ls -A cache
output
rmdir: failed to remove 'cache': Directory not empty
.gitkeep

Plain ls shows nothing, yet rmdir refuses: ls -A reveals a dotfile, a common placeholder for keeping empty directories in version control.

Remove a chain of empty directories

Setup run before this example
mkdir -p build/out/tmp
rmdir -pv build/out/tmp
explain
output
rmdir: removing directory, 'build/out/tmp'
rmdir: removing directory, 'build/out'
rmdir: removing directory, 'build'

-p removes build/out/tmp, then build/out, then build, stopping at the first one that is not empty. -v reports each removal.

Remove only the directories that are empty

Setup run before this example
mkdir empty1 empty2
rmdir --ignore-fail-on-non-empty empty1 empty2 logs
echo "exit status: $?"
ls -d */
output
exit status: 0
data/
logs/
photos/
scripts/
src/

The two empty directories are removed, logs is skipped silently, and the exit status is 0. The listing confirms only the fixture's own directories remain. Without the option, rmdir would print an error for logs and return 1.

Remove every empty directory in a tree

Setup run before this example
mkdir -p cache/a/b cache/c && touch cache/c/keep.txt
find cache -type d -empty -print -delete
find cache
output
cache/a/b
cache/a
cache
cache/c
cache/c/keep.txt

find -delete works depth-first, so cache/a/b is removed before cache/a, which is then empty too. cache/c keeps its file, so it and cache stay.

Common pitfalls

  • A directory that looks empty to ls can hold dotfiles; ls -A dir shows them and explains why rmdir refuses.

  • rmdir dir/ and rmdir dir are the same, but rmdir link-to-dir fails: it will not follow a symlink.

GNU vs BSD and macOS

The examples above ran the GNU/Linux version. These are the differences that matter on macOS and the BSDs (written from their documentation, not run here).

  • --ignore-fail-on-non-empty and --verbose are GNU long options; BSD/macOS rmdir has -p and (on recent versions) -v, but no ignore option.

Options worth knowing

A selection, described in our own words. For the full list, see rmdir(1) or rmdir --help.

OptionWhat it does
-p, --parentsAlso remove each parent directory that becomes empty.
-v, --verbosePrint a line for each directory removed.
--ignore-fail-on-non-emptySkip non-empty directories without an error.

Related