rmdir Command in Linux With Examples - GeeksforGeeks (2024)

Last Updated : 03 Jul, 2024

Improve

In the field of Linux, management of directories is a fundamental aspect of system administration and everyday tasks. One of the essential commands for directory management is rmdir, which is used to remove directories. In this guide, we’ll explore the rmdir command in detail, covering its usage, options, examples, and execution.

Table of Content

  • What is rmdir Command?
  • Options of rmdir Command
  • Examples of rmdir Command in Linux
  • Difference between rmdir and rm Command
  • Trouble shooting Issues with rmdir Command
  • Best practices for safely removing Directories

What is rmdir Command?

The rmdir command in Linux is specifically designed for removing empty directories. Unlike the rm command, which can delete both files and directories, rmdir focuses solely on directories. It is a straightforward tool but nonetheless crucial for maintaining a tidy directory structure on your system. It ensures whether the directories are empty or not before deleting it and it will delete only if those are empty. It helps to prevent data loss by accidentally removing the data contained directories.

Syntax

The basic syntax of the rmdir command is:

rmdir [option] directory_name

Here, directory_name refers to the name of the directory you want to remove.

Options of rmdir Command

The following are the options of rmdir command:

OptionDescription
--ignore-fail-on-non-emptyIt prevents the errors if the directory is not empty.
-p or --parentsIt will removes the directory and its parent directories if they are empty.
-v or --verboseIt helps in displaying the message for each directory that is removed.
--helpIt will displays the helpful information and exits.
--versionIt displays version information and exits.

Examples of rmdir Command in Linux

rmdir command is similar to the rm command, but rmdir only removes empty directories. So first, we will use the help flag to list down all the available options for the rmdir command:

rmdir --help

rmdir Command in Linux With Examples - GeeksforGeeks (1)

Example 1: The Basic rmdir Command

Let’s start the examples with a section with the simple rmdir command to remove multiple directories, and here is the basic syntax:

rmdir mydir1 mydir2 mydir3 .....
  • Here we will remove LINUX, INFO, and DETAIL directories through the following command:
rmdir LINUX INFO DETAIL

rmdir Command in Linux With Examples - GeeksforGeeks (2)

Example 2: The -p Option

You can use the -p option with the rmdir command to delete a directory, including all the subdirectories:

rmdir -p mydir1/mydir2/mydir3/...../mydirN
  • For example, we will delete the LINUX directory, including all its all ancestors, through the following command:
rmdir -p LINUX/mydir1/mydir2/mydir3

rmdir Command in Linux With Examples - GeeksforGeeks (3)

Example 3: The -v Option

  • If you want the terminal to display the message after removing the directory, you can use the -v option with the rmdir command:
rmdir -v dir1 dir2 dir3
  • Let’s now delete the LINUX, INFO, and DETAIL directories and display the message after their successful removal:
rmdir -v LINUX INFO DETAIL
rmdir: removing directory, 'LINUX'
rmdir: removing directory, 'INFO'
rmdir: removing directory, 'DETAIL'

rmdir Command in Linux With Examples - GeeksforGeeks (4)

Example 4: Remove Multiple Directories With the Same Expression

  • You can delete multiple directories if they have the same expressions by using the * in the rmdir command. For example, let’s remove all those directories which contain LINUX in their name:
ls 
LINUX1
LINUX2
LINUX3

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

rmdir Command in Linux With Examples - GeeksforGeeks (5)

  • In the above command, we have used the ls command to list all the available directories. Moreover, we executed the rmdir command -v option and * to delete all those directories which contain the same expression.

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

  • Sometimes you get the following error while removing a directory through the rmdir command:
rmdir <option> <directory> 
rmdir: failed to remove 'dir1': Directory not empty
  • So, in this case, you can use the –ignore-fail-on-non-empty to ignore the occurrences due to the non-empty directories. For instance, let’s remove the LINUX directory that contains sub-directories:
rmdir --ignore-fail-on-non-empty LINUX

rmdir Command in Linux With Examples - GeeksforGeeks (6)

Difference between rmdir and rm Command

The following are the difference between rmdir and rm command:

Aspectrmdir Commandrm Command
Primary FunctionIt removes the empty directoriesIt will remove the files and directories
Usage LimitationIt can only remove empty directoriesIt can remove both empty and non-empty entities
Recursive DeletionNot applicable to this commandIt can delete directories and their contents recursively with -r option
SafetyIt is safer, as it only deletes if directories are emptyIt requires caution, especially with -r and -f options
Command ComplexitySimple and straightforwardMore complex with multiple options and flags for different use cases

Trouble shooting Issues with rmdir Command

The following are the some of the trouble shooting issues regarding with rmdir command:

1. Directory Not Empty Error

Issue: rmdir fails with an error if the directory is not empty.

Solution: It ensuring that the directory should be empty before attempting to remove it. You can use rm -r command if you need to delete non-empty directories.

2. Permission Denied

Issue: Insufficient permissions to remove the directory.

Solution: Firstly check and modify the directory permissions or use sudo rmdir for having the necessary administrative privileges.

3. Directory Not Found

Issue: Specified directory does not exist or the path is incorrect.

Solution: Verify whether the directory path that is mentioned is existing or not.

4. Using –ignore-fail-on-non-empty

Issue: rmdir returns errors when directories are not empty.

Solution: Try to usse the --ignore-fail-on-non-empty option to suppress errors for non-empty directories, although it won’t remove them.

Best practices for safely removing Directories

The following are the some of the best practices for removing the directories safely:

  • Check Directory Contents: Firstly before deleting the directories, verify whether the directory is empty or not because rmdir is only helpful in deleting the empty directories..
  • Use rmdir for Empty Directories: Try to prefer using rmdir for removing empty directories to minimize the risk of accidentally deleting important files.
  • Use rm -r with Caution: When using rm -r command, we delete non-empty directories. This command should be used with caution, double-check the command and the directory path to ensure you do not remove critical files.
  • Backup Important Data: Try on always backup important data before performing deletion operations, especially when dealing with directories that may contain valuable information.

Conclusion

This was a brief explanation of the rmdir command in Linux with examples. We have mentioned every option you can try while removing an empty directory from the terminal. If you are a beginner, you may receive errors while removing an empty or a non-empty directory. That’s we have included the explanation on the –ignore-fail-on-non-empty option you can use if you get an error while using the rmdir command. Moreover, you can use multiple options of the rmdir command to remove the directories per the requirements.

Linux rmdir – FAQs

How to remove a directory in Linux?

On using the command like`rmdir <directory_name>` we can remove an empty directory, and we can use `rm -r <directory_name>` to remove a non-empty directory with all its contents.

What is the rmdir command in Linux?

`rmdir` is a command in Linux that is used for removing empty directories.

How do I delete a full directory in rmdir?

You can’t delete a full directory with `rmdir` as it only removes empty directories, For this situation try to use `rm -r <directory_name>` with caution for deleting a directory with all its contents.

How do I completely empty a directory in Linux?

On using the`rm -r <directory_name>/*` we can recursively delete all files and subdirectories within a directory, leaving the directory structure intact.

How do I delete a non-empty directory?

Use `rm -r <directory_name>` to delete a non-empty directory and all its contents recursively.



R

rahulkumarmandal

Improve

Previous Article

rmmod command in Linux with Examples

Next Article

rsync command in Linux with Examples

Please Login to comment...

rmdir Command in Linux With Examples - GeeksforGeeks (2024)

References

Top Articles
Top War Wiki
天瓏網路書店 | Comptia Linux+ Certification Companion: Hands-On Preparation to Master Linux Administration
Houses For Sale 180 000
NO CLUE: deutsche Übersetzung von NCT 127
Att Login Prepaid
Friscolawnmowing
2167+ Unique Pieces of Authentic Mid Century Modern Furniture In Stock - furniture - by dealer - sale - craigslist
Erhöhte Gesundheitsgefahr durch Zuckeraustauschstoff Erythrit?
Becu Turbotax Discount Code
Europese richtlijn liften basis voor Nederlandse wet - Liftinstituut - Alles voor veiligheid
United Center Section 305
Lebron Vs Pacers Stats
Northamptonshire | England, Map, History, & Facts
My Scheduler Hca Cloud
5 Best Brokerage Accounts for High Interest Rates on Cash Sweep - NerdWallet
Circloo Unblocked
Lynette Mettey Feet
Craigslist Apartment Los Angeles
Craigslist Goats For Sale By Owner Near Me
Exploring IranProud: A Gateway to Iranian Entertainment
Moss Adams Client Portal
Busse Bladeforums
Vision Government Solutions Stamford Ct
Mexi Unblocked Games
Broyhill Gazebo Instructions
How To Get Genji Cute Spray
Cronología De Chelsea Contra Fulham
Provo Craigslist
Ansos Umm
Herdis Eriksson Obituary
352-730-1982
On-Campus Student Employment
Is Jamie Kagol Married
Colorado Pick 3 Lottery
What is IXL and How Does it Work?
Heffalumps And Woozles Racist
Carabao Cup Wiki
Joftens Notes Skyrim
Urbn Employee Appreciation Fall 2023
Metro By T Mobile Sign In
Cnas Breadth Requirements
Indian River County FL.
Arre St Wv Srj
123Movies Scary Movie 2
Gizmo Ripple Tank Answer Key
Why Did Jen Lewis Leave Wavy 10
Transportationco.logisticare
Halloween 1978 Showtimes Near Movie Tavern Little Rock
Codex Genestealer Cults 10th Edition: The Goonhammer Review
Henry Ford Connect Email
Dark Pictures Wiki
Sammyflood
Latest Posts
Article information

Author: Patricia Veum II

Last Updated:

Views: 5884

Rating: 4.3 / 5 (64 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Patricia Veum II

Birthday: 1994-12-16

Address: 2064 Little Summit, Goldieton, MS 97651-0862

Phone: +6873952696715

Job: Principal Officer

Hobby: Rafting, Cabaret, Candle making, Jigsaw puzzles, Inline skating, Magic, Graffiti

Introduction: My name is Patricia Veum II, I am a vast, combative, smiling, famous, inexpensive, zealous, sparkling person who loves writing and wants to share my knowledge and understanding with you.