Linux Command rmdir Usage and Its 3 Options

Managing files and directories in Linux is very important. Among these, the linux command rmdir, used for removing empty directories, is frequently utilized. In this post, we will explore the basic usage of the Linux Command rmdir, along with various options and usage methods.

What is the Linux Command rmdir?

The rmdir command stands for “remove directory” and is used to delete empty directories in Linux. One important point to note is that the rmdir command can only delete empty directories. If there are files or other directories within a directory, deletion is not possible.

Basic Usage of rmdir

The basic usage of the rmdir command is very simple. It follows the format below:

rmdir [options] directory_name
ShellScript

For example, to delete an empty directory named example, you would enter:

rmdir example
ShellScript

The image below shows the state before and after deleting the example directory.

Figure 1. Deleting the example directory using the Linux Command rmdir
Figure 1. Deleting the example directory using the Linux Command rmdir

rmdir Options

-v Option (verbose): Detailed Information on Deletion

Using the -v option provides detailed information about the deleted directories. The following code uses a wildcard (*) to delete all directories starting with test:

rmdir -v test*
ShellScript

The image below displays the results of deleting all directories starting with test, indicating which directories were deleted. Since test.txt is a file and not a directory, the deletion failed.

Figure 2. Detailed information of deleted directories using the Linux Command rmdir -v
Figure 2. Detailed information of deleted directories using the Linux Command rmdir -v

-p Option (parents): Deleting Parent Directories

The -p option not only deletes empty directories but also deletes their parent directories if they are empty as well. This option is useful for deleting nested empty directories all at once.

rmdir -p example/subdir
ShellScript

This command deletes both subdir and its parent directory example if they are empty.

The image below shows the result of executing the above command. The directory structure was checked using the tree command. Since there are no files in the subdirectories of the example directory and subdir, both were deleted.

Figure 3. Deleting both parent and child directories using the Linux Command rmdir -p
Figure 3. Deleting both parent and child directories using the Linux Command rmdir -p

–ignore-fail-on-non-empty Option: Ignore Non-Empty Directories

The –ignore-fail-on-non-empty option ignores error messages when a directory is not empty. This option can be useful when deleting multiple directories in a script.

rmdir --ignore-fail-on-non-empty example
ShellScript

The above command continues even if the example directory is not empty.

Below, since the -v option was used, it indicates that the example directory is being deleted. However, since there is a file named a.txt in the example directory, it was not deleted.

Figure 4. Ignoring non-empty directories using the Linux Command rmdir --ignore-fail-on-non-empty
Figure 4. Ignoring non-empty directories using the Linux Command rmdir –ignore-fail-on-non-empty

Precautions When Using rmdir

Only Empty Directories Can Be Deleted

The rmdir command can only delete empty directories. If a directory contains files or other directories, it cannot be deleted, and an error message will be displayed.

Permission Issues

If you do not have permission to delete a directory, you cannot use the rmdir command. In this case, you can use the sudo command to gain permission.

sudo rmdir example
ShellScript

Irreversible Deletion

Directories deleted with the rmdir command cannot be recovered. Of course, if you delete a single directory, you can recreate it using the mkdir command. However, if you delete a complex directory structure, recovering it can be quite cumbersome. Therefore, use the rmdir command with caution.

Usage Methods of rmdir

Cleaning Up Empty Directory Structures

The rmdir command is useful for cleaning up unused empty directories in project directories or workspaces. By regularly deleting empty directories, you can keep your directory structure tidy.

Use in Scripts

The rmdir command can be used in scripts that automatically manage many files or directories. For example, it can be used to clean up empty directories after a backup job.

The following script finds and deletes all empty directories in the /backup directory.

#!/bin/bash

# Delete all empty directories in the backup directory
find /backup -type d -empty -exec rmdir {} \;
ShellScript

Using the -r option with the rm command deletes the specified directory and all of its subdirectories and files. While the rmdir command can only delete empty directories, the rm command can delete non-empty directories, so use it with particular caution.

For detailed usage of the rm command, refer to “How to Use the Linux Command rm and Its 4 Options

Summary

The rmdir command is an essential tool for deleting empty directories in Linux. While its basic usage is simple, using various options allows for more efficient directory management. Try using the rmdir command when you want to clean up empty directories.

Properly utilizing Linux commands can make system management much easier. If you have any further questions or want to learn more, feel free to ask!

I hope this post helps you in your Linux usage!

References

Leave a Comment