Sometimes we need to delete a directory that isn't empty , and we get permission errors like "Permission denied" or "Directory not empty" when we try to do so. This is because the directory isn't empty, and perhaps one of the directories or files it contains has special permissions that are causing the error. But it's possible to delete it easily with the mini-tutorial we're about to show you, so these messages won't stop you.
In a previous tutorial, we saw how to delete directories with a considerable amount of content—that is, directories with a size of several GB—without impacting system performance. We achieved this using Ionice, a very practical tool that allows us to control the priority of certain I/O transactions, similar to how Nice does with processes. Today, we'll focus on another simple action, but one that might be causing some problems for Linux novices.
The steps to follow when we try to delete our directory that is not empty would be to try with:
rmdir /mi-directorio
But in that case we can receive the error message we were talking about. To do it correctly, you can try the following:
rm -rfv /mi-directorio
What we get with these rm options is a recursive deletion for the contents of the directory and also force deletion respectively. The -v is simply the verbose mode of the command to display more information on standard output.
In the event that we still get the permission error, because we will try to obtain privileges so that we no longer have problems when trying to delete it, you can do it with su, or more recommended with sudo:
sudo rm -rfv /mi-directorio
I hope this has helped you with those stubborn directories that won't delete…