rmdir command on Linux (2024)

Introduction

Before we discuss rmdir command in Linux, let's briefly understand - What is rmdir command ?

The rmdir command on Linux is used to remove directories or folders from the file system. It is a built-in command that helps users delete empty directories quickly and efficiently.

Unlike the rm command, which deletes files, rmdir focuses specifically on removing directories. By using rmdir, users can clean up their directory structure and streamline their file system organization, making it a convenient tool for managing directories with ease.

In this tutorial, you will understand rmdir command in Linux. We will also address a few FAQs on rmdir command in Linux.

Introduction to rmdir Command

In Linux, empty directories are specifically meant to be removed using the rmdir command. rmdir only works with directories, as opposed to the rm command, which can remove both files and directories. It's a simple tool, but it's essential for keeping your system's directory structure organized.

Basic Syntax

The rmdir command's basic syntax is as follows:

rmdir [option] directory_name

The name of the directory you wish to remove is indicated by directory_name in this case.

rmdir Command

Similar to the rm command, the rmdir command only deletes empty directories. Therefore, we will first list all the options available for the rmdir command using the help flag:

rmdir --help

The command above shows the different options, including:

Understanding Options

  • -p, --parents: When the specified directory is removed, this option allows parent directories to be removed as well, in case they become empty. If the parent directories of the directory become empty, it effectively removes both of them.
  • -v, --verbose: When activated, this option gives feedback on the action taken by displaying a message for every directory processed.
  • --ignore-fail-on-non-empty: By default, if the directory is not empty, rmdir fails. This option, however, forces rmdir to carry out the removal despite such errors.
  • --version: This option shows the version details and closes the window.

Exit Status

This command returns the following exit values:

ItemDescription
0The operation to remove each directory entry specified by the Directory parameter was completed successfully.
>0An error occurred.

Example 1: The Basic rmdir Command

To begin the examples, let's create a section using the basic rmdir command to remove multiple directories. The syntax is as follows:

rmdir mydir1 mydir2 mydir3 .....

Here, we'll use the following command to delete the LINUX, INFO, and DETAIL directories:

rmdir LINUX INFO DETAIL

Example 2: The -p Option

The rmdir command can be used with the -p option to remove a directory along with all of its subdirectories:

rmdir -p mydir1/mydir2/mydir3/...../mydirN

For instance, we will use the following command to remove the LINUX directory and all of its ancestors:

rmdir -p LINUX/mydir1/mydir2/mydir3

Example 3: The -v Option

You can use the -v option with the rmdir command to have the message displayed in the terminal after the directory is removed:

rmdir -v dir1 dir2 dir3

Now that the LINUX, INFO, and DETAIL directories have been successfully removed, let's delete them and display the following message:

rmdir -v LINUX INFO DETAILrmdir: removing directory, 'LINUX'rmdir: removing directory, 'INFO'rmdir: removing directory, 'DETAIL'

Example 4: Remove Multiple Directories With the Same Expression

If several directories have the same expressions, you can delete them all by using the * in the rmdir command. For illustration, let's delete every directory whose name includes LINUX:

ls LINUX1LINUX2LINUX3rmdir -v LINUX*rmdir: removing directory, 'LINUX1'rmdir: removing directory, 'LINUX2'rmdir: removing directory, 'LINUX3'

The ls command was used in the command above to list every directory that is available. Additionally, we used the rmdir command with the -v option and * to remove every directory that contained the same expression.

Example 5: The --ignore-fail-on-non-empty Option

When using the rmdir command to remove a directory, you may occasionally encounter the following error:

rmdir <option> <directory> rmdir: failed to remove 'dir1': Directory not empty

To ignore the occurrences caused by the non-empty directories in this situation, use the --ignore-fail-on-non-empty option. Let's take out the LINUX directory, for example, which has the following subdirectories:

rmdir --ignore-fail-on-non-empty LINUX

FAQs on rmdir command on Linux

Can I delete non-empty directories with rmdir?

No, rmdir can only delete empty directories. To delete directories with contents, you can use the rm -r command.

Is there a way to force delete directories with rmdir?

No, the rmdir command does not have a force delete option. It can only delete empty directories.

How do I delete multiple directories at once with rmdir?

To delete multiple directories simultaneously, you can provide multiple directory names as arguments to the rmdir command.

Can I delete directories with special characters in their names using rmdir?

Yes, you can delete directories with special characters by enclosing the directory name in quotes when executing the rmdir command.

What happens if I try to delete a non-existent directory with rmdir?

If you attempt to delete a directory that doesn't exist, rmdir will return an error message indicating that the directory could not be found.

Does rmdir provide any options to display additional information?

rmdir is a simple command without many options. It mainly focuses on deleting empty directories, so it does not provide additional information.

Are there any potential risks when using the rmdir command?

The main risk when using the rmdir command is accidentally deleting directories that contain important files. It's essential to double-check before executing the command to ensure that you are deleting the intended directories and that they are empty to avoid unintentional data loss.

Conclusion

We hope this tutorial helped you understand how to use rmdir command in Linux.

If you have any queries, please leave a comment below, and we’ll be happy to respond to them for sure.

rmdir command on Linux (2024)

References

Top Articles
1Tamilmv.kids
Keith D. Biglow Funeral Directors - Tulsa | Tulsa, Oklahoma
Raleigh Craigs List
Everything you need to know about a Sam's Club Membership
Superhot Unblocked Games
Puss In Boots: The Last Wish Showtimes Near Fox Berkshire
Python Regex Space
Ups Drop Off Newton Ks
Culver's Flavor Of The Day Little Chute
Palos Health My Chart
What Is Opm1 Treas 310 Deposit
Best Conjuration Spell In Skyrim
Paul Mccombs Nashville Tn
Pollen Count In Brandon Fl
Caldwell Idaho Craigslist
Hannaford Weekly Flyer Manchester Nh
Lorain County Busted Mugshots
Roses Gordon Highway
Gas Buddy Prices Near Me Zip Code
suggest - Englisch-Deutsch Übersetzung | PONS
Excuse Me This Is My Room Comic
Dead By Daylight Subreddit
Fortnite Chapter 5: All you need to know!
Zen Leaf New Kensington Menu
Monahan's By The Cove Charlestown Menu
Reapers Tax Barotrauma
Vision Government Solutions Stamford Ct
Stronghold Slayer Cave
Reptile Expo Spokane
AC Filters | All About Air Filters for AC | HVAC Filters
Joshua Efird Net Worth
Author T. Jefferson Parker
Currently Confined Coles County
Shaw Funeral Home Vici Oklahoma
三上悠亜 Thank You For Everything Mikami Yua Special Photo Book
Forums Social Media Girls Women Of Barstool
Jodie Sweetin Breast Reduction
10000 Blaulicht-Meldungen aus Baden-Württemberg | Presseportal
R Mcoc
Alaska State Troopers Dispatch
Appsanywhere Mst
marie claire Australia January 2016 Robyn Lawley, Rachel Taylor, Sarah Snook • EUR 11,50
Po Box 6726 Portland Or 97228
Publix Coral Way And 147
Hourly Pay At Dick's Sporting Goods
Blood Types: What to Know
Erin Mclaughlin Eyebrow
Privateplaygro1
What Does Code 898 Mean On Irs Transcript
Omaha World-Herald from Omaha, Nebraska
Pollen Count Butler Pa
Navy Qrs Supervisor Answers
Latest Posts
Article information

Author: Gov. Deandrea McKenzie

Last Updated:

Views: 6382

Rating: 4.6 / 5 (66 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Gov. Deandrea McKenzie

Birthday: 2001-01-17

Address: Suite 769 2454 Marsha Coves, Debbieton, MS 95002

Phone: +813077629322

Job: Real-Estate Executive

Hobby: Archery, Metal detecting, Kitesurfing, Genealogy, Kitesurfing, Calligraphy, Roller skating

Introduction: My name is Gov. Deandrea McKenzie, I am a spotless, clean, glamorous, sparkling, adventurous, nice, brainy person who loves writing and wants to share my knowledge and understanding with you.