Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Shell Scripting
  • Kali Linux
  • Ubuntu
  • Red Hat
  • CentOS
  • Docker in Linux
  • Kubernetes in Linux
  • Linux interview question
  • Python
  • R
  • Java
  • C
  • C++
  • JavaScript
  • DSA
Open In App
Next Article:
rsync command in Linux with Examples
Next article icon

rmdir Command in Linux With Examples

Last Updated : 01 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In Linux, managing directories efficiently is a crucial skill for both system administrators and everyday users. The ‘rmdir’ command is a specialized tool designed specifically for removing empty directories, helping to maintain a clean file system and avoid accidental data loss. This guide delves into the usage, options, and practical examples of the ‘rmdir’ command, offering insights into its safe and effective application.

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]...

Here, ‘[directory]…’ refers to one or more directories you wish to remove.

Options of rmdir Command

The following are the options of rmdir command:

Option Description
--ignore-fail-on-non-empty It prevents the errors if the directory is not empty.
-p or --parents It will removes the directory and its parent directories if they are empty.
-v or --verbose It helps in displaying the message for each directory that is removed.
--help It will displays the helpful information and exits.
--version It 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 --help

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 linux info details

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
removing the parent directories

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'
removing directories with verbose

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'
removing multiple directories

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
ignoring fail non empty option

Difference between ‘rmdir’ and ‘rm’ Command

The following are the difference between rmdir and rm command:

Aspect rmdir Command rm Command
Primary Function It removes the empty directories It will remove the files and directories
Usage Limitation It can only remove empty directories It can remove both empty and non-empty entities
Recursive Deletion Not applicable to this command It can delete directories and their contents recursively with -r option
Safety It is safer, as it only deletes if directories are empty It requires caution, especially with -r and -f options
Command Complexity Simple and straightforward More 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. 



Next Article
rsync command in Linux with Examples

R

rahulkumarmandal
Improve
Article Tags :
  • Linux-Unix
  • linux-command
  • Linux-directory-commands
  • Linux-file-commands

Similar Reads

  • rcp Command in Linux with examples
    When working in a Linux environment, there often comes a time when you need to transfer files from one computer to another. While more secure options like scp or rsync exist, the rcp (Remote Copy Protocol) command offers a simple and efficient way to copy files between systems, especially for beginn
    5 min read
  • read command in Linux with Examples
    read command in the Linux system is used to read from a file descriptor. This command reads up the total number of bytes from the specified file descriptor into the buffer. If the number or count is zero, this command may detect errors. But on success, it returns the number of bytes read. Zero indic
    3 min read
  • readelf command in Linux with Examples
    When we compile source code, an object file is generated of the program and with the help of linker, this object files gets converted to a binary file which, only the machine can understand. This kind of file follows some structures one of which is ELF(Executable and Linkable Format). And to get the
    4 min read
  • readlink command in Linux with Examples
    The 'readlink' command in Linux is a valuable tool used to print resolved symbolic links or canonical file names. In simpler terms, when dealing with symbolic links and you need to know the actual path they represent, the 'readlink' command reveals the path of the symbolic link. This command is part
    3 min read
  • Reboot Linux System Command with Examples
    Rebooting a Linux system is a fundamental administrative task necessary for applying updates, troubleshooting, or system maintenance. Various commands are available to reboot a Linux system, each with specific options and use cases. This guide explores essential reboot commands such as `reboot`, `sh
    11 min read
  • How to Rename File in Linux | rename Command
    Changing the names of files in Linux is something we often do, and the "rename" command is like a helpful friend for this job. This guide is like a journey to becoming really good at renaming files on Linux, showing you how handy and useful the "rename" command can be. Whether you're just starting o
    8 min read
  • reset command in Linux with Examples
    reset command in the Linux system is used to initialize the terminal. This is useful once a program dies leaving a terminal in an abnormal state. Note that you may have to type reset to get the terminal up and work, as carriage-return may no longer work in the abnormal state. Also, the terminal will
    3 min read
  • restore command in Linux with Examples
    restore command in Linux system is used for restoring files from a backup created using dump. The restore command performs the exact inverse function of dump. A full backup of a file system is being restored and subsequent incremental backups layered is being kept on top of it. Single files and dire
    5 min read
  • return command in Linux with examples
    return command is used to exit from a shell function. It takes a parameter [N], if N is mentioned then it returns [N] and if N is not mentioned then it returns the status of the last command executed within the function or script. N can only be a numeric value. The return command is especially usefu
    2 min read
  • rev command in Linux with Examples
    rev command in Linux is used to reverse the lines characterwise. This utility reverses the order of the characters in each line by copying the specified files to the standard output. If no files are specified, then the standard input will be read. Here, we will explore the syntax, examples, and opti
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences