LinuxCommandLibrary

rmdir

Remove empty directories

TLDR

Remove specific directories

$ rmdir [path/to/directory1 path/to/directory2 ...]
copy

Remove specific nested directories recursively
$ rmdir [[-p|--parents]] [path/to/directory1 path/to/directory2 ...]
copy

Clean a directory of empty directories
$ rmdir *
copy

SYNOPSIS

rmdir [OPTION]... DIRECTORY...

PARAMETERS

-p, --parents
    Remove DIRECTORY and its ancestor directories if they become empty after the removal. For example, rmdir -p a/b/c will remove c, then b if b is empty, then a if a is empty.

-v, --verbose
    Output a diagnostic message for every directory processed, indicating success or failure.

--ignore-fail-on-non-empty
    Suppress error messages when attempting to remove a directory that is not empty. The command will still fail to remove the non-empty directory but won't output an error to standard error.

--help
    Display a help message and exit.

--version
    Output version information and exit.

DESCRIPTION

The rmdir command is a standard Unix and Linux utility used to remove empty directories from the file system. Unlike rm -r, rmdir will only succeed if the specified directory contains no files or subdirectories. This makes it a safer option for cleaning up directory structures, preventing accidental deletion of important data. It's commonly used for tidying up empty branches of a directory tree, either manually or within shell scripts. When a directory is successfully removed, rmdir provides no output unless the --verbose option is used.

CAVEATS

  • rmdir can only remove directories that are completely empty. If a directory contains any files or subdirectories (even hidden ones), rmdir will fail with an error.
  • To remove non-empty directories, the rm -r command must be used, which is significantly more powerful and potentially dangerous, requiring extreme caution.
  • You must have appropriate permissions: write permission in the parent directory of the directory you wish to remove, and execute permission for the directory itself.

RECURSIVE EMPTY DIRECTORY REMOVAL

While rmdir itself does not have a recursive option to traverse and empty non-empty directories, its --parents option allows it to remove an entire path of empty directories. For more complex recursive cleanups of truly empty directories, rmdir is often combined with find. For example, find . -type d -empty -delete is a common and efficient way to remove all empty subdirectories within a given path. Alternatively, find . -type d -empty -exec rmdir {} + can be used, although -delete is often preferred for simplicity and efficiency where available.

EXIT STATUS

rmdir returns an exit status of 0 upon successful completion. A non-zero exit status indicates an error, such as trying to remove a non-empty directory, a directory that does not exist, or a permissions issue. This behavior is crucial for scripting, allowing conditional execution based on the command's outcome.

HISTORY

rmdir is a foundational command in Unix-like operating systems, part of the core utilities since the early versions of Unix. It was designed to provide a specific, safe mechanism for directory removal, distinct from the more general-purpose rm command. Its core functionality has remained remarkably consistent, reinforcing its role as a simple yet essential tool for managing the file system hierarchy.

SEE ALSO

mkdir(1), rm(1), find(1)

Copied to clipboard