Linux rmdir Command Examples for Beginners (2024)

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.

Linux rmdir Command Examples for Beginners (1)

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.

Linux rmdir Command Examples for Beginners (2)

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/dir5rmdir: 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.

Linux rmdir Command Examples for Beginners (3)

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 dir1rmdir: 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.

Linux rmdir Command Examples for Beginners (4)

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.

Linux rmdir Command Examples for Beginners (5)

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.

Linux rmdir Command Examples for Beginners (2024)

References

Top Articles
DPH Laws and Regulations: Rabies
Who is Evan Spiegel, the CEO of Snap? | Clay
Spasa Parish
The Machine 2023 Showtimes Near Habersham Hills Cinemas
Gilbert Public Schools Infinite Campus
Rentals for rent in Maastricht
159R Bus Schedule Pdf
11 Best Sites Like The Chive For Funny Pictures and Memes
Finger Lakes 1 Police Beat
Craigslist Pets Huntsville Alabama
Paulette Goddard | American Actress, Modern Times, Charlie Chaplin
Red Dead Redemption 2 Legendary Fish Locations Guide (“A Fisher of Fish”)
What's the Difference Between Halal and Haram Meat & Food?
Rugged Gentleman Barber Shop Martinsburg Wv
Jennifer Lenzini Leaving Ktiv
Havasu Lake residents boiling over water quality as EPA assumes oversight
Justified - Streams, Episodenguide und News zur Serie
Epay. Medstarhealth.org
Olde Kegg Bar & Grill Portage Menu
Half Inning In Which The Home Team Bats Crossword
Amazing Lash Bay Colony
Cato's Dozen Crossword
Cyclefish 2023
What’s Closing at Disney World? A Complete Guide
New from Simply So Good - Cherry Apricot Slab Pie
Ohio State Football Wiki
Find Words Containing Specific Letters | WordFinder®
FirstLight Power to Acquire Leading Canadian Renewable Operator and Developer Hydromega Services Inc. - FirstLight
Webmail.unt.edu
When Is Moonset Tonight
Navy Qrs Supervisor Answers
Trade Chart Dave Richard
Sweeterthanolives
How to get tink dissipator coil? - Dish De
Lincoln Financial Field Section 110
1084 Sadie Ridge Road, Clermont, FL 34715 - MLS# O6240905 - Coldwell Banker
Kino am Raschplatz - Vorschau
Classic Buttermilk Pancakes
Pick N Pull Near Me [Locator Map + Guide + FAQ]
'I want to be the oldest Miss Universe winner - at 31'
Gun Mayhem Watchdocumentaries
Ice Hockey Dboard
Infinity Pool Showtimes Near Maya Cinemas Bakersfield
Dermpathdiagnostics Com Pay Invoice
A look back at the history of the Capital One Tower
Alvin Isd Ixl
Maria Butina Bikini
Busted Newspaper Zapata Tx
Rubrankings Austin
2045 Union Ave SE, Grand Rapids, MI 49507 | Estately 🧡 | MLS# 24048395
Upgrading Fedora Linux to a New Release
Latest Posts
Article information

Author: Madonna Wisozk

Last Updated:

Views: 6386

Rating: 4.8 / 5 (68 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Madonna Wisozk

Birthday: 2001-02-23

Address: 656 Gerhold Summit, Sidneyberg, FL 78179-2512

Phone: +6742282696652

Job: Customer Banking Liaison

Hobby: Flower arranging, Yo-yoing, Tai chi, Rowing, Macrame, Urban exploration, Knife making

Introduction: My name is Madonna Wisozk, I am a attractive, healthy, thoughtful, faithful, open, vivacious, zany person who loves writing and wants to share my knowledge and understanding with you.