Master the Linux ‘rmdir’ Command: A Comprehensive Guide

Peter Hou
2 min readMay 21, 2023

--

This guide provides an in-depth overview of the rmdir command in Linux. It covers its history, when and why to use it, its syntax and parameters, common use cases, and advanced tips. The guide also highlights the importance of having the necessary permissions to remove a directory and the fact that rmdir only removes empty directories. Understanding how to use rmdir effectively will help you manage your filesystem more efficiently.

Instructions

This guide provides detailed instructions on the Linux rmdir command. It covers its history, uses, syntax, parameters, common use cases, advanced tips, and important notes. The goal is to help beginners understand and effectively use the rmdir command.

History

The rmdir command, short for "remove directory", has been part of Unix-like operating systems since the early versions of Unix. It was incorporated into Linux as a basic file management tool, and is now part of the GNU core utilities package.

When and why to use it

The rmdir command is used to delete empty directories in Unix and Unix-like operating systems, including Linux. It's a simple and straightforward way to manage filesystem space and organization, particularly useful when cleaning up unnecessary or obsolete directories.

How to use it

To use rmdir, you simply pass the name of the directory you wish to remove. The basic syntax is:

$ rmdir directory_name

If the operation is successful, rmdir will not output any message.

The commonly used parameters

  • p, --parents: This parameter removes DIRECTORY and its ancestors. For example, rmdir -p dir/subdir/subsubdir will remove the directories subsubdir, subdir, and dir if they are all empty.
$ rmdir -p dir/subdir/subsubdir

Other supported parameters

  • --ignore-fail-on-non-empty: This parameter allows you to ignore errors that occur due to non-empty directories.
  • --verbose: This parameter outputs a diagnostic for every directory processed.

Most common use casesMost common use cases

The most common use case for rmdir is the deletion of a single empty directory:

$ rmdir test_directory

In this case, test_directory is removed if it’s empty.

The tricky skills

The rmdir command is quite straightforward, but there’s one trick you might find useful: removing multiple directories at once. You can do this by separating each directory with a space. For example:

$ rmdir directory1 directory2 directory3

This will attempt to remove directory1, directory2, and directory3.

What needs to be noted

Remember that rmdir only removes empty directories. If you need to remove a directory and its contents, you’ll need to use rm -r instead. Also, you will need the appropriate permissions to remove a directory.

Conclusion

The rmdir command is a simple, yet essential tool for managing your filesystem in Linux. By understanding its syntax and parameters, you can efficiently clean up your directory structure.

--

--

Peter Hou

I am a Senior Software Engineer and tech lead in a top tech company.