$linuxjunkies
>

rmdir(1)

Remove empty directories from the filesystem.

UbuntuDebianFedoraArch

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

FlagWhat it does
-p, --parentsremove DIRECTORY and its ancestors; mkdir -p dir1/dir2/dir3 && rmdir -p dir1/dir2/dir3 removes all three
-v, --verboseprint a message for each directory removed
--ignore-fail-on-non-emptydo not report error if directory is not empty; useful in scripts
--helpdisplay help message and exit
--versionoutput version information and exit

Examples

Remove the empty directory 'mydir'; fails if mydir contains any files

rmdir mydir

Remove three empty directories and print confirmation for each removal

rmdir -v dir1 dir2 dir3

Remove 'build', then 'src', then 'project' if each is empty (clean up nested empty directories)

rmdir -p project/src/build

Create nested directories then remove them all with parent flag

mkdir -p a/b/c && rmdir -p a/b/c

Attempt 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

Related commands