Guide to Linux rmdir Command With Examples | Baeldung on Linux (2024)

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, rmdirdeletes 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 dir1,dir2, anddir3, 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 dir1rmdir: 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/dir2rmdir: 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:

  • findsearches for filesystem objects
  • .specifies the current working directory as the search location
  • -type d -emptytells 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.

Guide to Linux rmdir Command With Examples | Baeldung on Linux (2024)

References

Top Articles
Destiny 2: Top 10 Grenade Launchers, Ranked
Cryptids 101 - The Ultimate Guide (With Cryptid List)
Gfr Soccer
Meet Scores Online 2022
Gay Black Scat
Angelaalvarez Leak
The Canterville Ghost Showtimes Near Northwoods Cinema 10
Chs.mywork
Guide:Guide to WvW Rewards
Plan the Ultimate Trip to Lexington, Kentucky
Best Charter Schools Tampa
Kamala Harris is making climate action patriotic. It just might work
My Big Fat Greek Wedding 3 Showtimes Near Regal Ukiah
Spirited Showtimes Near Gqt Kalamazoo 10
Bunni.soph
Acuity Eye Group - La Quinta Photos
Brise Stocktwits
How Much Is Cvs Sports Physical
What Is My Walmart Store Number
Imperious Skyrim
Wildflower | Rotten Tomatoes
Craigslist St. Paul
Beaver Dam Locations Ark Lost Island
Twitter claims there’s “no evidence” 200 million leaked usernames and email addresses came from an exploit of its systems
Eros Cherry Hill
Pearl City Hall Pearl Ms
Rolling-Embers Reviews
Think Up Elar Level 5 Answer Key Pdf
South Park Old Fashioned Gif
Kathy Carrack
Our Favorite Paper Towel Holders for Everyday Tasks
The Legend of Maula Jatt | Rotten Tomatoes
Glassbox Eyecare
Duluth Craigslist Boats
Craigslist Farm And Garden Yakima
Tires Shop Santoyo
Fuzz Bugs Factory Hop Halloween
Secondary Math 2 Module 3 Answers
Brooklyn Park City Hall
24 Hour Pharmacy Berkeley
The Top 6 Most Expensive Hermès Birkin Bags
Skip The Games Albany
GW2 Fractured update patch notes 26th Nov 2013
Who To Start for Fantasy Football Friday Night Football: Week 1 (2024)
Ttw Cut Content
Antonin Balthazar Lévy
Evangeline Shrine Club Banquet Hall Photos
Senna Build Guides :: League of Legends Strategy Builds, Runes, Items, and Abilities :: Patch 14.18
Transportationco.logisticare
I spruced up my kitchen for £131 - people can’t believe it’s the same room
10 Ways to Fix a Spacebar That's Not Working Properly
Navy Qrs Supervisor Answers
Latest Posts
Article information

Author: Lilliana Bartoletti

Last Updated:

Views: 5702

Rating: 4.2 / 5 (53 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Lilliana Bartoletti

Birthday: 1999-11-18

Address: 58866 Tricia Spurs, North Melvinberg, HI 91346-3774

Phone: +50616620367928

Job: Real-Estate Liaison

Hobby: Graffiti, Astronomy, Handball, Magic, Origami, Fashion, Foreign language learning

Introduction: My name is Lilliana Bartoletti, I am a adventurous, pleasant, shiny, beautiful, handsome, zealous, tasty person who loves writing and wants to share my knowledge and understanding with you.