rmdir Command in Linux



The rmdir command, an abbreviation for "remove directory," is a fundamental command-line utility in Unix/Linux systems designed to delete empty directories. Unlike the rm command, which can remove files and directories regardless of their content, rmdir is specialized for eliminating directories that contain no files or subdirectories. This feature makes it a safer choice for routine cleanup tasks where there's no risk of inadvertently deleting non-empty directories.

Table of Contents

Here is a comprehensive guide to the options available with the rmdir command −

Syntax of rmdir Command in Linux

The basic syntax for the rmdir command is as follows −

rmdir [options] directory...

Where −

  • directory − The name of the directory you wish to remove.
  • options − Various flags and parameters to modify the command's behavior.

rmdir Command Options

Here are several options that can be utilized with the command rmdir on Linux −

Option Description
-p, --parents Remove the specified directory and its parent directories, if they become empty.
--ignore-fail-on-non-empty Ignore the failure that occurs when a directory is not empty.
-v, --verbose Output a message for each directory that is removed.
--help Display a help message and exit.
--version Output version information and exit.

Examples of rmdir Command in Linux

Here are some practical examples to illustrate how the Linux rmdir command can be effectively used −

  • Removing a Single Empty Directory
  • Verbosely Removing a Directory
  • Removing Parent Directories
  • Ignoring Failure on Non-Empty Directories

Removing a Single Empty Directory

Suppose you have an empty directory named empty_dir that you want to remove. You can achieve this by running the following command −

rmdir empty_dir

This command will delete the empty_dir directory, given that it contains no files or subdirectories.

rmdir Command in Linux1

Verbosely Removing a Directory

To remove a directory and output a message confirming the action, you can use the -v option −

This command will delete the empty_dir directory, given that it contains no files or subdirectories.

rmdir -v empty_dir

This command will delete the empty_dir directory, given that it contains no files or subdirectories.

rmdir Command in Linux2

Removing Parent Directories

If you want to remove a directory along with its parent directories, provided they are empty, you can use the -p option −

rmdir -p parent_dir/empty_dir/sub_empty_dir

This command will remove sub_empty_dir, empty_dir, and parent_dir if they all become empty as a result.

rmdir Command in Linux3

Ignoring Failure on Non-Empty Directories

To attempt to remove a directory and ignore the error if it is not empty, you can use the --ignore-fail-on-non-empty option −

rmdir --ignore-fail-on-non-empty non_empty_dir

This command will try to remove non_empty_dir but will not report an error if the directory is not empty.

rmdir Command in Linux4

Conclusion

The rmdir command in Linux is an invaluable tool for managing directories, providing a safe and straightforward method to remove empty directories. By understanding and utilizing the various options available, you can efficiently handle directory removal tasks with precision and confidence.

Whether you need to delete a single empty directory, remove parent directories, or obtain help and version information, the rmdir command offers the flexibility to meet your needs.

Advertisements