rmdir(1)
Remove empty directories from the filesystem.
Synopsis
rmdir [OPTION]... DIRECTORY...Description
rmdir deletes one or more empty directories. A directory must be completely empty (containing no files or subdirectories) to be removed; if it contains anything, rmdir will fail with an error.
Use rm -r to recursively delete directories with contents. For removing a single empty directory, rmdir is safer because it refuses to delete non-empty directories.
Common options
| Flag | What it does |
|---|---|
-p, --parents | remove DIRECTORY and its ancestors; mkdir -p dir1/dir2/dir3 && rmdir -p dir1/dir2/dir3 removes all three |
-v, --verbose | print a message for each directory removed |
--ignore-fail-on-non-empty | do not report error if directory is not empty; useful in scripts |
--help | display help message and exit |
--version | output version information and exit |
Examples
Remove the empty directory 'mydir'; fails if mydir contains any files
rmdir mydirRemove three empty directories and print confirmation for each removal
rmdir -v dir1 dir2 dir3Remove 'build', then 'src', then 'project' if each is empty (clean up nested empty directories)
rmdir -p project/src/buildCreate nested directories then remove them all with parent flag
mkdir -p a/b/c && rmdir -p a/b/cAttempt removal and print custom message if it fails
rmdir empty_dir 2>/dev/null || echo 'Directory not empty'Alternative: recursively find and delete all empty directories (use find, not rmdir)
find . -type d -empty -delete