Remove directory using the RMDIR command in Linux

Home > Search > Linux commands
  by

The rmdir command can be used to remove directories. Let's take an example where there is a directory named file1 at /home/john.doe/bin.

[john.doe@server1 ~]# ls -l
drwxrwxr-x.  2  john.doe  john.doe  6  May 23 19:06  bin

 


Remove a directory that is empty

If the directory is empty, the rmdir command followed by the name of the empty directory can be used to remove the directory.  In this example, the empty /home/john.doe/bin directory is removed.

[john.doe@server1 ~]# rmdir /home/john.doe/bin

 

The bin directory no longer exists.

[john.doe@server1 ~]# ls -l

 


Remove a directory that is not empty

If the directory is not empty, the rmdir command followed by the name of the not empty directory will display an error. In this example, the /home/john.doe/bin directory is not empty. The rm command can be used to remove directories that are not empty.

[john.doe@server1 ~]# rmdir /home/john.doe/bin
rmdir: failed to remove '/home/john.doe/bin': Directory not empty

 

The bin directory still exists.

[john.doe@server1 ~]# ls -l
drwxrwxr-x.  2  john.doe  john.doe  6  May 23 19:06  bin

 

The --ignore-fail-on-non-empty option can be used to not display an error if the rmdir command fails to remove a directory because it is not empty.

[john.doe@server1 ~]# rmdir --ignore-fail-on-non-empty /home/john.doe/bin

 

However, the directory will not be removed.

[john.doe@server1 ~]# ls -l
drwxrwxr-x.  2  john.doe  john.doe  6  May 23 19:06  bin

 

 


Remove another users file

By default, you can remove empty directories you own, and you cannot remove empty directories you do not own. For example, if jane.doe attempts to remove /home/john.doe/bin, which is owned by john.doe, an error will appear.

[jane.doe@server1 ~]# rmdir /home/john.doe/bin
rmdir: cannot remove '/home/john.doe/bin': Permission denied

 

If jane.doe is a member of the sudo group, jane.doe can remove the empty directory using the sudo command.

[jane.doe@server1 ~]# sudo rmdir /home/john.doe/bin

 

Root can remove any empty directory without having to use sudo.

[root@server1 ~]# rmdir /home/john.doe/bin


Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter in the box below so that we can be sure you are a human.




Comments