1. Introduction

In Linux filesystems, directories are important in organizing files and data. As we work with files and directories, it’s common to have empty and unnecessary directories that accumulate over time, leading to a cluttered and inefficient filesystem. To that end, one effective way to remove empty directories is by using the rmdir command.

In this tutorial, we’ll first cover the basics of the rmdir command. Next, we’ll discuss its various options. Lastly, we compare it with the rm command which can also remove directories.

2. Basic Usage

To begin with, let’s understand the basic syntax of the rmdir command:

rmdir [options] directory

Here, options refer to various flags of the rmdir command. They are followed by a directory path we want to remove.

2.1. Removing a Single Empty Directory

To remove a single empty directory named dir1, present in the current working directory, we just supply the respective name:

$ rmdir dir1

When we execute this command, rmdir deletes dir1 without providing any information or output. Additionally, if we want to remove empty directories from a different location, we need to provide the full path.

2.2. Removing Multiple Empty Directories

In case we want to remove multiple empty directories, we can delete all of them by providing their names separated by a space or by using wildcard (*):

$ rmdir dir1 dir2 dir3
$ rmdir *

Thus, the first command deletes any empty directories among dir1dir2, and dir3, while the second deletes any empty directories in the current working directory.

2.3. Verify Main Functionality

Next, we can verify whether the rmdir only deletes empty directories by testing it on a non-empty directory:

$ rmdir dir1
rmdir: failed to remove 'dir1': Directory not empty

As expected, the commands return an error.

3. Additional Options

Now, let’s discuss some of the options of the rmdir command to further customize its behavior and enhance functionality.

3.1. Using the -v Option

As discussed earlier, the rmdir command silently deletes any specified empty directory. However, in some cases, we may want to verify that the directory has been removed successfully.

This is the function of the -v option which stands for verbose to display detailed output:

$ rmdir -v *
rmdir: removing directory, 'dir1'
rmdir: removing directory, 'dir2'
rmdir: removing directory, 'dir3'

The result clearly shows that the command deleted three empty directories.

3.2. Using the -p Option

The -p option stands for parents and we can use it to remove a directory with all its parent directories as long as they’re empty. So, the -p option is particularly useful when we want to remove a directory structure that’s no longer needed.

For example, let’s say we want to remove the parent directory named dir1 with an empty subdirectory dir2 using a single command:

$ rmdir -p -v dir1/dir2
rmdir: removing directory, 'dir1/dir2'
rmdir: removing directory, 'dir1'

When we execute the command, it first deletes the subdirectory and then deletes the parent directory, since it’s now empty.

3.3. Using the –ignore-fail-on-non-empty Option

As we saw earlier, the rmdir command displays an error message when we try to delete a non-empty directory. However, if we want to suppress this error message, we can use the –ignore-fail-on-non-empty option.

For example, let’s say we want to remove a non-empty directory dir1:

$ rmdir --ignore-fail-on-non-empty dir1
$

In this case, we don’t see the error message and dir1 remains untouched, with its content intact.

4. Using the find Command with rmdir

Sometimes, we may not know the exact location of the empty directories, and finding them manually could be time-consuming.

In such a situation, we can use the find and rmdir commands together to first locate and then remove those empty directories:

$ find . -type d -empty -exec rmdir {} +

Now, let’s understand the command:

  • find searches for filesystem objects
  • . specifies the current working directory as the search location
  • -type d -empty tells find to consider only empty directories
  • -exec rmdir {} + executes the rmdir command on any results

So, this command finds all empty directories and subdirectories within the current directory and deletes them using the rmdir command.

5. Comparison With rm

Although the rmdir command is a handy tool for removing empty directories and keeping the file system organized, it has certain limitations.

For example, it doesn’t contribute much to freeing up disk space, which is a common concern when low on storage. This is because empty directories typically don’t consume much space.

Instead, we can use the rm command to free up disk space by deleting non-empty directories. Importantly, we must exercise caution when using the rm command, as we can accidentally delete critical files and directories.

6. Conclusion

In this article, we explored the rmdir command, a powerful tool for efficiently managing the Linux file system.

The basic function of the rmdir command is to remove empty directories. The -v option displays the details of all the directories that we’re removing. Further, we can remove the complete empty directory structure by using the -p option. Lastly, we use the –ignore-fail-on-non-empty option to ignore errors that may arise when we remove non-empty directories.

Since removing empty directories doesn’t affect storage space much, the rm command is a better choice if we are low on storage and desire to free some space up.