Bootstrap FreeKB - Linux Commands - Remove directory using rmdir
Linux Commands - Remove directory using rmdir

Updated:   |  Linux Commands articles

The rmdir command can be used to remove directories. Let's say you want to remove the /tmp/foo directory. The rmdir command followed by /tmp/foo can be used to remove the directory.

rmdir /tmp/foo

 

Before removing a directory, you may want to use the ls (list) command with the -A or --almost-all flag piped to the wc (word count) command to determine if a directory is empty. This will return hidden files in the directory to ensure a directory containing hidden files is not empty. If the directory is empty, 0 0 0 should be returned.

~]# ls -A /tmp/foo | wc
0    0    0

 

If the /tmp/foo directory is empty, it will be removed, and the rmdir command will not produce any output. The ls (list) command with the -d or --directory flag can be used to determine if the directory was removed. If the directory was removed, and no longer exists, the following should be returned.

~]# ls -d /tmp/foo
ls: cannot access /tmp/foo: No such file or directory

 

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. The directory will not be removed.

rmdir --ignore-fail-on-non-empty /tmp/foo

 


Remove a directory that is not empty

The ls (list) command with the -A or --almost-all flag piped to the wc (word count) command can be used to determine if a directory is empty. This will return hidden files in the directory to ensure a directory containing hidden files is not empty. If the directory contains files or sub directories, values greater than 0 0 0 should be returned.

~]# ls -A /tmp/foo | wc
1    1    12

 

Attempting to remove a directory that contains one or more files or subdirectories will return the following.

~]# rmdir /tmp/bar
rmdir: failed to remove '/tmp/bar': Directory not empty

 

The rm command with the -r (recursive) and -f (force) flags can be used to remove directories that are not empty. This will remove the directory and everything at and below the directory.

rm -rf /tmp/bar

 


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

 


Remove directory that begins with a dash (-)

If there is a directory that starts with a dash, use this command.

rm -rf ./-16383792982

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


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