Files and directories
rmdir
Remove empty directories, and refuse if they are not empty.
rmdir [OPTION...] DIRECTORY...
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 oldrmdir old
ls -d old
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
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
rmdir: failed to remove 'notes.txt': Not a directory
rmdir only removes directories. Use rm for files.
Remove a chain of empty directories
Setup run before this example
mkdir -p build/out/tmprmdir -pv build/out/tmp
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 empty2rmdir --ignore-fail-on-non-empty empty1 empty2 logs
echo "exit status: $?"
ls -d */
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.txtfind cache -type d -empty -print -delete
find cache
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
lscan hold dotfiles;ls -A dirshows them and explains whyrmdirrefuses.rmdir dir/andrmdir dirare the same, butrmdir link-to-dirfails: 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-emptyand--verboseare GNU long options; BSD/macOSrmdirhas-pand (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.
| Option | What it does |
|---|---|
-p, --parents | Also remove each parent directory that becomes empty. |
-v, --verbose | Print a line for each directory removed. |
--ignore-fail-on-non-empty | Skip non-empty directories without an error. |