How to delete a folder with rmdir cmmand on Linux

Endrit Qerreti

Endrit Qerreti

In this tutorial, you will learn how to delete a foler on Linux, using the rmdir command. rmdir command is a useful command that you can use to delete folders on Linux.

The good thing about rmdir is that it is a safe command, which means you don't risk of deleting folders that have files on it. For example, let's say you want to clean your system and want to delete multiple folders, but you don't want to delete files accidentally during the cleanup, rmdir allows you do this without having to worry about files.

Delete an empty folder

To delete an empty folder, run the following command

rmdir foldername

Where foldername is the name of the folder that we are going to delete

If the folder foldername has a directory on it, or a file then the rmdir command will not delete this folder and it will show you the error as shown in the image below

In this case, the folder delete-me has another directory on it

Delete an empty folder and hide the error output

The error above can be ignored if you use the --ignore-fail-on-non-empty argument, for example, if you are mass deleting empty folders, and you don't want the errors to be displayed on the terminal then you can use

rmdir foldername --ignore-fail-on-non-empty

So if the foldername is a non empty folder, the rmdir command won't delete it, but it won't also show the error why the folder wasn't deleted.

Delete multiple empty folders

To delete multiple folders, you can use the following command

rmdir folder1 folder2 folder3

This command will remove folder1, folder2 and folder3 if they are empty, and if they are located on the same directory because notice how we are not specifying the path of folders.

Delete parent directories

To delete parent directories if empty use the following command

rmdir -p folder1/folder2/folder3

For example we have a folder called delete-me and inside this folder are 3 folders, folder1>folder2>folder3

So, folder1 contains folder2, folder2 contains folder3, and folder3 is empty, now to delete all these folders you can use the above command, and these folders will be deleted if empty.

I created a file on folder 3 and ran the command above, to reproduce the error

folder 1 and folder 2 were empty, while folder 3 not. So none of the folders got deleted

Use verbose command

If you want to know what directory is being deleted exactly, then you can use the verbose argument. This option shows an ouput for each directory that gets removed, it also shows the name of the directory on the output

rmdir delete-me --verbose


In this tutorial, you learned how to use the rmdir command on Linux to remove directories and parent directories if empty. rmdir command is powerful and safe command at the same time, so you can use it to clean your computer from junk folders. However, if you don't feel confident of using the rmdir command on a production system or your main system, then I'd recommend you play around with it on a virtual machine to understand this command better.