EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

rmdir command in Linux

By Priya PedamkarPriya Pedamkar

Home » Software Development » Software Development Tutorials » Linux Tutorial » rmdir command in Linux

rmdir-command-in-Linux

Introduction to rmdir command in Linux

We know that the Linux system is made up of files and directories. There may be plenty of directories in our system which is no longer needed. So we need to clean them up periodically. In Linux, there is a command called rmdir which is used to remove directories from our Linux file system. In this article, we will discuss how to use the rmdir command with various command line options.

rmdir Command Description

The rmdir command is used to remove empty directories from our Linux Operating System. Every directory that needs to be removed should not contain any files or subdirectories. Otherwise, the rmdir command cannot remove the directory from our file system.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

If the directory specified is not empty, then the rmdir command will not remove the directory and it will try to remove other specified directories in the command. The directories are removed in the order that they are specified in the command line i.e. from left to right.

To remove both the parent and subdirectory of the parent, the subdirectory should be mentioned first so that the parent directory is made empty and rmdir can remove it. The rmdir command is somewhat similar to rm -d command. If you desire to remove a non-empty directory, then you can use rm command with the recursive option.

Syntax

The syntax of the rmdir command is given below.

rmdir [-p] [-v, --verbose] [--ignore-fail-on-non-empty] [ directory names....]

Here p, v, and ignore-fail-on-non-empty are options used in the rmdir command.

Options

There are plenty of options that can be used with the rmdir command. Let us discuss them one by one.

Popular Course in this category
Linux Training Program (16 Courses, 3+ Projects)16 Online Courses | 3 Hands-on Projects | 160+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (6,013 ratings)
Course Price

View Course

Related Courses
Kali Linux Training (3 Courses, 3+ Projects)Red Hat Linux Training Program (4 Courses, 1+ Projects)
  • -help: This will display the syntax of the rmdir command along with the options. It will also provide a brief explanation about each option.
  • -p option: This option will remove the specified directory and its parent directories. In this option every specified directory name will be considered as the pathname of which all directories will be removed beginning from the last component.
  • –ignore-fail-on-non-empty: Usually when you try to remove a directory which is not empty using the rmdir command, it will produce an error message. This option will not report a failure message which occurs purely because of a non-empty directory.
  • -v, –verbose: This option displays verbose information of each directory that is being processed.
  • -v, –version: This option will print the information of the version and exit.

Examples of rmdir command in Linux

Let us discuss a few examples to get a better understanding of the rmdir command.

Example #1

rmdir sampledir1

The above command will remove the directory named sampledir1 if it is empty. If it is not empty then the error message will be produced.

In this case, you have to remove the directory contents before deleting it.

Example #2

To remove multiple directories at once use the following command.

rmdir sample1 sample2 sample3

The above command will remove all the directories sample1 sample2 and sample 3 if they are empty. If any of the directories is not empty then an error message will be displayed for that particular directory and other directories will be removed from the file system.

Output:

rmdir command in Linux-1.1

Example #3

rmdirdir/subdirect dir

The above command will remove the directory named dir/subdirect if the directory is empty and then directory dir will be removed if it is empty after dir/subdirect is removed.

Example #4

rmdir -p dir/subdirect

It is the same as the command shown in example 3. rmdir command will remove the directory dir/subdirect and then it will remove the dir directory.

How to Ignore Non-Empty Directories?

By default, if you are trying to remove a non-empty directory, the rmdir command will produce an error message. To ignore the error message that arises on deleting a non-empty directory, use the –ignore-fail-on-non-empty option.

Syntax:

$ rmdir--ignore-fail-on-non-empty test-dir

Output:

rmdir command in Linux-1.2

How to Remove Parent Directories Using rmdir command?

rmdir command can also perform its operation over the parent directories. It has the capacity to delete both the directory and its parent directories in one shot. You can make use of this feature with the help of-p option.

Consider the following example.

$ rmdir -p test/testdir/

The above command will delete both test and testdir directories. For this to happen, all parent directories should be empty other than the empty directory to be deleted.

How to Get Help for rmdir command

To get help about the rmdir command uses the following command.

$ rmdir --help

The following output is produced displaying a detailed description of the rmdir command.

Output:

rmdir command in Linux-1.3

You can also use the man command to display the manual pages of the rmdir command.

$ man rmdir

How to Display the Version Information of rmdir command?

To display the version information, use the –version option along with rmdir command as shown below.

$ rmdir --version

The following output is produced.

Output:

rmdir command in Linux-1.4

Difference Between rmdir and rm –r

You can also use the rm command to delete directories by using the -r option alongside the command. So what is the basic difference between both the commands? Well, the answer is simple. The rm command can be used to delete non-empty directories as well but rmdir command is used to delete only empty directories. There is absolutely no way to delete non-empty directories using the rmdir command.

Advantages of the Rmdir Command

  • rmdir command deletes only empty directories which helps us to avoid the accidental loss of our data. This is an important feature because in the Linux system once the data is deleted it is extremely difficult to recover the data.
  • It can delete multiple directories at once.
  • The rmdir command is simple and easy to use both by beginners and experts.

Recommended Articles

This is a guide to rmdir command in Linux. Here we also discuss the introduction and how to get help for rmdir command along with an example and its code implementation. You may also have a look at the following articles to learn more –

  1. Mkdir Command in Linux
  2. PS command in Linux
  3. Remove dir Linux
  4. Linux Filter Commands

Linux Training Program (16 Courses, 3+ Projects)

16 Online Courses

3 Hands-on Projects

160+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
Linux Tutorial
  • Linux Commands
    • CAT Command in Linux
    • PS command in Linux
    • Mkdir Command in Linux
    • rmdir command in Linux
    • mv command in Linux
    • Linux rm Command
    • CHMOD Command in Linux
    • Zip Command in Linux
    • Linux Route Command
    • SCP Command in Linux
    • du Command in Linux
    • Less Command in Linux
    • Copy Command in Linux
    • Head Command in Linux
    • Help Command in Linux
    • Linux History Command
    • hostname Command in Linux
    • GREP Command in Linux
    • Linux Curl Command
    • Linux List Users
    • Linux List Directories
    • Linux Directory Permissions
    • Find Directory Linux
    • Linux User Permission
    • Linux Add User
    • Linux Delete User
    • Linux uniq
    • Linux Filter Commands
    • Linux sort Command
    • Linux Sort by Size
    • Remove dir Linux
    • Linux tr Command
    • Linux Touch Command
    • Linux More Command
    • Linux Cal Command
    • Linux OD
    • Linux Zcat
    • Linux tracepath
    • Linux Sleep
    • Linux bg Command
    • Linux WC
    • Linux Read
    • Linux Write
    • Linux Rename Command
    • Linux Free Command
    • Linux tac
    • Linux Shred
    • Linux wget
    • Linux Date
    • Linux Untar
    • Linux JQ
    • Linux Xargs
    • Linux Mount Command
    • Linux mdadm
    • Linux comm
    • Linux tee Command
    • Tail Command in Linux
    • Linux gzip
    • Linux Directory Structure
    • Linux Alias Command
    • Linux Inode
    • Linux Ping
    • Linux Watch Command
    • Linux Split Command
    • Linux Size
    • Linux Network Stack
    • Linux Diff Command
    • Vim Command in Linux
    • Linux nslookup
  • Basic
    • Introduction to Linux
    • What is Linux
    • Careers in Linux
    • How to Install Linux
    • How to Install Ubuntu
    • How to Install Ubuntu Server
    • How To Install Debian
    • Advantage Of Linux
    • What is Raid in Linux
    • What is Raid 5?
    • Raid 5 vs Raid 6
    • Careers in Linux Administration
    • What is Shell in Linux?
    • Bash Shell in Linux
    • Linux Emulator
    • Linux Operators
    • Linux Apps
    • Linux Keyboard Shortcuts
    • Linux Crontab
    • What is Raid Storage
    • What is Vmware
    • What is RedHat
    • What is Debian
    • Uses of Ubuntu
    • Career In RedHat
    • Linux Distributions
    • Linux From Scratch
    • Linux Format
    • Cheat Sheet Linux
  • Advanced
    • Install VMware
    • Install GRUB
    • Chown Command in Linux
    • Linux Network Command
    • Configuring Linux Network Environment
    • DNS Configuration in Linux
    • Linux Users
    • Create User in Linux
    • DHCP Server in Linux
    • FTP Server in Linux
    • Linux Regular Expression
    • IP Command in Linux
    • Set IP Address in Linux
    • Tar Command in Linux
    • Dynamic Host Configuration Protocol
    • Linux Kill Signals
    • Linux File Permissions
    • Linux File System
    • Linux Create File
    • Linux Concatenate Files
    • Linux Join
    • Linux Process Management
    • Linux Proxy Server
    • Linux System Commands
    • Linux System Logging
    • NFS in Linux
    • Linux LVM
    • Linux Repository
    • Samba Server in Linux
    • Top Commands in Linux
    • Types of Shells in Linux
    • Shell Script Types For Linux
    • GCC Command in Linux
    • Monolithic Kernel
    • YARN Package Manager
    • Linux Sed Replace
    • Linux List Groups
    • Linux Container
    • Linux Find File by Name
    • Create Folder in Linux
    • Linux Move Folder
    • Linux Error Codes
    • Linux Automount
    • Linux HugePages
  • Kali Linux
    • Install Kali Linux
    • Kali Linux Uses
    • Kali Linux Commands
    • Kali Linux Repository
    • Kali Linux Packages
    • Kali Linux root password
    • Kali Linux Alternative
  • Interview Questions
    • Linux Interview Questions
    • Linux System Administration Interview
    • Red Hat Interview Questions

Related Courses

Kali Linux Training

Linux Training Course

Red Hat Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - Linux Training Program (16 Courses, 3+ Projects) Learn More