In this guide, check out how to remove non-empty directories in Linux.
Removing a Directory in Linux
First, let’s have a look at how to remove directories in Linux.
Removing an Empty Directory
In this example, I have set an empty directory:
To remove the directory, we can use rmdir:
Removing a Non-Empty Directory
When it comes to a non-empty directory, the previously mentioned methods won’t work.
Here, I’ve set a non-empty directory for demonstration:
Try to remove normally, use the following command:
To remove the non-empty directory, use the following rm command instead:
- -r: Instructs rm to recursively delete the contents of the directory, both the files and sub-directories.
- -v: Instructs rm to run in verbose mode.
If you don’t want any prompt for action, then add the flag “-f”, as well:
If you want to have prompts for actions, then use the flag “-i” instead. Note that rm will ask for all the files and sub-directories it’s about to remove:
Instead of “-i”, the flag “-I” is less intrusive while still protecting against mistakes:
In Linux, removing a directory is different than removing files. The removal tools and methods are also different depending on whether the target directory is empty or not. This guide demonstrates how to remove both empty and non-empty directories. Note that the directories and the data contained within and removed in this manner will have no way of being recovered except by using special tools and methods. Thus, before removing, ensure that it’s not something important.
The man pages of rm and rmdir contain further information about various supported options: