As Linux users, we frequently interact with files and directories, and one common task is removing directories from the file system. However, we must exercise caution when performing directory removal to avoid data loss.
In this beginner-friendly article, we will explore the rmdir command, which is specifically designed for removing directories. We will also provide practical examples that can be applied in day-to-day usage.
The syntax of the rmdir command follows the pattern of other Linux commands, consisting of two parts: options and arguments. It can be represented as:
$ rmdir [OPTIONS] ... <DIRECTORY1> <DIRECTORY2> ...
In the syntax, the square brackets ([]) denote optional arguments, while the angular brackets (<>) indicate mandatory arguments.
Basic Usage of rmdir Command in Linux
The primary purpose of the rmdir command is to delete directories. However, it’s crucial to understand that it can only remove empty directories. In this section, we will explore the fundamental usage of the rmdir command.
Delete an Empty Directory in Linux
To begin, let’s create several empty directories:
$ mkdir dir1 dir2 dir3 dir4
Now, let’s confirm that the directories have been successfully created:
$ ls -l
Next, we’ll remove the dir1
directory and verify its removal:
$ rmdir dir1
$ ls -l
Similarly, we can use the rmdir command to delete multiple empty directories simultaneously. Let’s remove the remaining directories:
$ rmdir dir2 dir3 dir4
Finally, let’s confirm that all the directories have been successfully removed:
$ ls -l
As a result, we can observe that the ls
command no longer displays any directories.
![](https://serverconfig.net/wp-content/uploads/2023/06/Linux-rmdir-Command-Usage.png)
rmdir Verbose Mode
In the earlier section, we relied on the ls
command to confirm the removal of directories. However, executing an additional command solely for verification purposes may seem redundant.
To address this, we can make use of the verbose mode by enabling the -v
option. This option provides diagnostic information for each directory that is being processed.
To recreate the directory structure we previously created, execute the following command:
$ mkdir dir1 dir2 dir3 dir4
Now, let’s remove the directories while enabling the verbose mode:
$ rmdir -v dir1 dir2 dir3 dir4
Upon executing the above command, you will see diagnostic information for each directory being processed. Finally, you can verify the removal of the directories using the ls -l
command. Based on the output, you can conclude that all the directories have been successfully removed.
Remove Empty Sub-Directories in Linux
We often create sub-directories on a file system, which allows us to organize our data in a proper way. Let’s see how to work with empty sub-directories.
As discussed in the first example, we can remove multiple directories using the rmdir command. However, the situation becomes tricky when sub-directories are large in numbers.
In such cases, we can use the -p
option, which removes the directory and all its ancestors. Let’s understand this with an example.
First, create a sub-directory structure:
$ mkdir -p dir1/dir2/dir3/dir4/dir5
Let’s remove all these directories in one go:
$ rmdir -p -v dir1/dir2/dir3/dir4/dir5 rmdir: removing directory, 'dir1/dir2/dir3/dir4/dir5' rmdir: removing directory, 'dir1/dir2/dir3/dir4' rmdir: removing directory, 'dir1/dir2/dir3' rmdir: removing directory, 'dir1/dir2' rmdir: removing directory, 'dir1'
No Comments
Leave a comment Cancel