rmdir – Used to Remove Empty Directories

The rmdir command is used to remove empty directories in Linux. This command only removes empty directories, which means that it will not delete any directories that contain files or subdirectories. If you attempt to delete a non-empty directory using rmdir, you will receive an error message.

Overview

The basic syntax for the rmdir command is as follows:

rmdir [OPTION]... DIRECTORY...

Here, DIRECTORY refers to the name of the directory or directories that you want to remove. You can specify multiple directories to be removed at once by separating their names with spaces.

For example, to remove a directory named mydir, you would use the following command:

rmdir mydir

If you want to remove multiple directories at once, you can include their names as separate arguments:

rmdir dir1 dir2 dir3

If the directory that you want to remove is not located in your current working directory, you can specify its full path:

rmdir /path/to/mydir

By default, rmdir does not provide any output when it successfully removes a directory. However, you can use the -v option to display a message for each directory that is removed:

rmdir -v mydir

Specific Use Cases

Here are a few examples of how you might use the rmdir command in practice:

  • If you have a directory that you no longer need, you can use rmdir to remove it. For example, if you have a directory named oldstuff that you want to delete, you would use the following command: rmdir oldstuff.
  • If you have a script that creates temporary directories during its operation, you can use rmdir to remove those directories once the script has completed. For example, if your script creates a directory named tempdir, you could include the following command at the end of the script to remove it: rmdir tempdir.
  • If you want to remove multiple directories at once, you can include their names as separate arguments. For example, if you want to remove directories named dir1, dir2, and dir3, you could use the following command: rmdir dir1 dir2 dir3.

Options

Here are the available options for the rmdir command:

Option Description
-p, --parents Remove DIRECTORY and its ancestors.
-v, --verbose Output a message for each directory that is removed.
--help Display help information and exit.
--version Display version information and exit.

Troubleshooting Tips

If you attempt to remove a non-empty directory using rmdir, you will receive an error message. To remove a directory that contains files or subdirectories, you should use the rm command instead.

If you receive a message that says “Directory not empty” when trying to remove a directory using rmdir, it means that the directory is not empty and cannot be removed. You can use the ls command to list the contents of the directory and determine what files or subdirectories are preventing it from being removed.

Notes

  • Be careful when using the rmdir command, as it will permanently delete any empty directories that you specify. Make sure that you have a backup of any important files or directories before using this command.
  • If you want to remove a directory and all of its contents, including non-empty subdirectories, you should use the rm command with the -r or -R option instead.