How to Remove a Non-Empty Directory in Linux

2

In Linux, every time you need to delete a document, you’d use the rm command. When it involves deleting a listing, then again, issues get a bit of complicated. To delete a listing, there’s a devoted software rmdir that may take away empty directories. What if the objective listing accommodates a host of useless information?

In this information, take a look at how to take away non-empty directories in Linux.

Removing a Directory in Linux

First, let’s take a look at how to take away directories in Linux.

Removing an Empty Directory

In this situation, I’ve set an empty listing:

To take away the listing, we will be able to use rmdir:

How to Remove a Non-Empty Directory in Linux How to Remove a Non-Empty Directory in Linux 1632158575 739 How to Remove a Non Empty Directory in Linux

Removing a Non-Empty Directory

When it involves a non-empty listing, the prior to now discussed strategies received’t paintings.

Here, I’ve set a non-empty listing for demonstration:

How to Remove a Non-Empty Directory in Linux How to Remove a Non-Empty Directory in Linux 1632158575 897 How to Remove a Non Empty Directory in Linux

Try to take away usually, use the next command:

How to Remove a Non-Empty Directory in Linux How to Remove a Non-Empty Directory in Linux 1632158576 574 How to Remove a Non Empty Directory in Linux

How to Remove a Non-Empty Directory in Linux How to Remove a Non-Empty Directory in Linux 1632158576 501 How to Remove a Non Empty Directory in Linux

To take away the non-empty listing, use the next rm command as an alternative:

How to Remove a Non-Empty Directory in Linux How to Remove a Non-Empty Directory in Linux 1632158576 820 How to Remove a Non Empty Directory in Linux

Here:

  • -r: Instructs rm to recursively delete the contents of the listing, each the information and sub-directories.
  • -v: Instructs rm to run in verbose mode.

If you don’t need any recommended for motion, then upload the flag “-f”, as smartly:

$ rm -vrf

How to Remove a Non-Empty Directory in Linux How to Remove a Non-Empty Directory in Linux 1632158576 445 How to Remove a Non Empty Directory in Linux

If you need to have activates for movements, then use the flag “-i” as an alternative. Note that rm will ask for all of the information and sub-directories it’s about to take away:

$ rm -vri

How to Remove a Non-Empty Directory in Linux How to Remove a Non-Empty Directory in Linux 1632158576 360 How to Remove a Non Empty Directory in Linux

Instead of “-i”, the flag “-I” is much less intrusive whilst nonetheless protective in opposition to errors:

$ rm -vrI

How to Remove a Non-Empty Directory in Linux How to Remove a Non-Empty Directory in Linux 1632158577 795 How to Remove a Non Empty Directory in Linux

Conclusion

In Linux, casting off a listing is other than casting off information. The elimination equipment and techniques also are other relying on whether or not the objective listing is empty or now not. This information demonstrates how to take away each empty and non-empty directories. Note that the directories and the information contained inside of and got rid of on this approach will don’t have any means of being recovered except for by means of the usage of particular equipment and techniques. Thus, prior to casting off, make certain that it’s now not one thing necessary.

The guy pages of rm and rmdir include additional details about quite a lot of supported choices:

How to Remove a Non-Empty Directory in Linux How to Remove a Non-Empty Directory in Linux 1632158577 807 How to Remove a Non Empty Directory in Linux

How to Remove a Non-Empty Directory in Linux How to Remove a Non-Empty Directory in Linux 1632158577 561 How to Remove a Non Empty Directory in Linux

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More