How to Use the rmdir Command in Linux?

By | January 12, 2022
rmdir Command in Linux

Removing a directory or a file is a frequent operation that you do in your Linux machine.

You may create a new directory and end up removing it completely from your machine. For GUI folks, it is quite easy to perform these operations. You just need to right-click on that directory and choose the option to remove it completely.

Another way of removing a file or directory on Linux is via the command-line interface i.e. the terminal. Although this is not the popular option as doing the aforesaid with the GUI is much easier, having knowledge of how to remove files using the command-line interface might be demanded by certain situations.

One such scenario is when working with virtual machines where you have limited access to the GUI. In this article, we will be discussing a few methods and options related to removing a directory from your system using the rmdir command.

What is a Directory?

A directory is just a set of files and sub-directories. In Linux, everything is structured into files and directories. This creates a tree-like structure, which makes it easy to understand the flow and structure of a particular project.

rm Command in Linux

rm is a contraction for “remove” in Linux. Among the vast array of Linux shell commands, the rm command comes under the subcategory of file management commands. The Linux command removes a file or set of files. If you enter the man rm command on the command line, it will explain how to use the rm command, its options and its arguments.

rmdir command

rmdir is an important extension of rm. The rmdir command is used to delete empty directories. The Linux command will delete the directory if and only if it is empty. The syntax and usage of the command can be checked using the following command:

Vamware
rmdir --help

Using the -p Option

Every directory parameter is handled as a pathname in this option and all directories in the path will get deleted. Below is an implementation of this command:

Vamware

Using the -v Option

Using this option with the rmdir command provides the message of directory deletion. Let’s see an example for this:

Deleting Multiple Subdirectories in the Same Directory

You can also use the rmdir command to delete many subdirectories simultaneously. Below is an implementation of this approach:

Vamware

Conclusion

In this article, we discussed the rmdir command in Linux. Deleting a (set of) directory is a common task while working on Linux-based projects. This task can be accomplished by using the rmdir command.

Please note that the Linux shell command only deletes empty directories and skips deleting non-empty directories. You can also delete multiple directories in the same directory using the rmdir command. Also, to get the deletion verification message, use the -v option.

People are also reading:

Author: Vinay Singh

I am a Full Stack Developer with a Bachelor's Degree in Computer Science, who also loves to write technical articles that can help fellow developers.

Leave a Reply

Your email address will not be published.