In Linux, whenever you want to delete a file, you’d use the rm command. When it comes to deleting a directory, however, things get a bit complex. To delete a directory, there’s a dedicated tool rmdir that can remove empty directories. What if the target directory contains a bunch of unnecessary files?

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:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/remove-non-empty-directory-linux-01.png" data-lazy- height="287" src="data:image/svg xml,” width=”1899″>

To remove the directory, we can use rmdir:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/remove-non-empty-directory-linux-02.png" data-lazy- height="175" src="data:image/svg xml,” width=”1895″>

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:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/remove-non-empty-directory-linux-03.png" data-lazy- height="612" src="data:image/svg xml,” width=”1899″>

Try to remove normally, use the following command:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/remove-non-empty-directory-linux-04.png" data-lazy- height="212" src="data:image/svg xml,” width=”1893″>

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/remove-non-empty-directory-linux-05.png" data-lazy- height="212" src="data:image/svg xml,” width=”1897″>

To remove the non-empty directory, use the following rm command instead:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/remove-non-empty-directory-linux-06.png" data-lazy- height="529" src="data:image/svg xml,” width=”1895″>

Here:

  • -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:

$ rm -vrf

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/remove-non-empty-directory-linux-07.png" data-lazy- height="533" src="data:image/svg xml,” width=”1902″>

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:

$ rm -vri

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/remove-non-empty-directory-linux-08.png" data-lazy- height="512" src="data:image/svg xml,” width=”1895″>

Instead of “-i”, the flag “-I” is less intrusive while still protecting against mistakes:

$ rm -vrI

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/remove-non-empty-directory-linux-09.png" data-lazy- height="579" src="data:image/svg xml,” width=”1895″>

Conclusion

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:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/remove-non-empty-directory-linux-10.png" data-lazy- height="1043" src="data:image/svg xml,” width=”1897″>

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/remove-non-empty-directory-linux-11.png" data-lazy- height="1047" src="data:image/svg xml,” width=”1895″>

About the author

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/profile-photo-1-150×150.jpg614a53f3d7909.jpg" height="112" src="data:image/svg xml,” width=”112″>

Sidratul Muntaha

Student of CSE. I love Linux and playing with tech and gadgets. I use both Ubuntu and Linux Mint.