As Linux users, we interact with the files and directories on a regular basis. One common operation users perform is removing directories from the file system. However, we have to be extra careful while removing the directories. Because carelessly performed removal operations can result in data loss.
In this beginner-friendly article, we will learn about the rmdir command. We will also discuss some of the practical examples that can be used on a day-to-day basis.
The syntax of the rmdir command is similar to other Linux commands. At a high level, it is divided into two parts – options and arguments:
$ rmdir [OPTIONS] ... <DIRECTORY1> <DIRECTORY2> ...
Here, the square brackets ([])
represent the optional arguments whereas angular brackets (<>)
represent the mandatory arguments.
Basic Usage of rmdir Command in Linux
As the name suggests, the rmdir command is used to remove the directory. However, it is important to note that it can remove empty directories only. In this section, we will see the basic usage of the rmdir command.
Delete an Empty Directory in Linux
First, create a few empty directories:
$ mkdir dir1 dir2 dir3 dir4
Let’s verify that the required directories have been created:
$ ls -l
Now, let’s remove the dir1 directory and verify that it has been removed:
$ rmdir dir1 $ ls -l
In a similar fashion, we can use the rmdir command to remove multiple empty directories at once.
Let’s remove the remaining directories:
$ rmdir dir2 dir3 dir4
Finally, verify that all the directories have been removed:
$ ls -l
Here, we can see that the ls command doesn’t show any directory.
rmdir Verbose Mode
In the previous section, we used the ls command to verify the directory removal. However, it doesn’t make sense to execute one more command just to verify the actions of the previous commands.
In such cases, we can enable the verbose mode using the -v
option, which provides diagnostics for every processed directory.
Let’s create the same directory structure that we created previously:
$ mkdir dir1 dir2 dir3 dir4
Now, let’s remove the directories with the verbose mode enabled:
$ rmdir -v dir1 dir2 dir3 dir4 $ ls -l
From the above output, we can conclude that all the directories have been 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
In this example, we have used the -p
option with the mkdir command to create a sub-directory structure.
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'
Here, the verbose mode removes the dir5 directory and all its ancestor directories.
Handle Directory Not Empty Failure
We already know that the rmdir can remove only empty directories. Any attempt to remove a non-empty directory will result in an error. Though this provides protection against data loss, in some rare cases it can create an issue.
For example, if we try to remove a non-empty directory from the script that is getting executed by Jenkins then the job will report a failure.
To simulate this, let’s try to remove the non-empty directory:
$ mkdir -p dir1/dir2/dir3/dir4/dir5 $ rmdir dir1 rmdir: failed to remove 'dir1': Directory not empty
For such error cases, we can use the --ignore-fail-on-non-empty
option, which ignores all the failures that happened due to a non-empty directory.
Let’s use this option with the command and check the return value:
$ rmdir --ignore-fail-on-non-empty dir1 $ echo $? 0
In this example, we can see that the command didn’t report any error and the zero return value indicates the successful command execution. However, it is important to note that this option just suppresses the error and doesn’t remove the non-empty directory.
Use Regular Expressions in rmdir Command
Similar to other Linux commands, we can use regular expressions with the rmdir command. Let’s see the usage of the following two regular expressions:
?
– It matches exactly one character.*
– It matches zero or more occurrences of the previous characters.
First, create a few empty directories:
$ mkdir dir1 dir2 dir-01 dir-02
Now, let’s use the '?'
regular expression with the string ‘dir’ to remove the dir1 and dir2 directories:
$ rmdir -v dir? rmdir: removing directory, 'dir1' rmdir: removing directory, 'dir2'
Here, we can see that the command removed the correct directories.
Next, use the '*'
regular expression to remove the other two directories:
$ rmdir -v dir-* rmdir: removing directory, 'dir-01' rmdir: removing directory, 'dir-02'
In this example, we can see that the other two directories have been removed.
In this section, we discussed the usage of only two regular expressions. However, we can also use the other advanced regular expressions with the rmdir command.
In this article, first, we saw the basic usage of the rmdir command. Then we discussed the verbose mode and removal of the sub-directories. Next, we saw how to handle failures when a directory is not empty. Finally, we discussed how to use regular expressions.