How to delete a non-empty directory in Terminal?
How do I delete the following directory?
I typed:
rmdir lampp
This error comes up:
rmdir: failed to remove `lampp': Directory not empty
Is there a command to delete all the files in the directory and delete the directory folder?
Solution 1:
Use the below command :
rm -rf lampp
It deletes all files and folders contained in the lampp
directory.
In case user doesn't have the permission to delete the folder:
Add sudo
at the beginning of the command :
sudo rm -rf folderName
Otherwise, without sudo
you will be returned permission denied. And it's a good practice to try not to use -f
while deleting a directory:
sudo rm -r folderName
Note: this is assuming you are already on the same level of the folder you want to delete in terminal, if not:
sudo rm -r /path/to/folderName
FYI: you can use letters -f
, -r
, -v
:
-
-f
= to ignore non-existent files, never prompt -
-r
= to remove directories and their contents recursively -
-v
= to explain what is being done
Solution 2:
rm -R lampp
However, you need to be careful with a recursive command like this, as it's easy to accidentally delete a lot more than you intended.
It is a good idea to always double-check which directory you're in, and whether you typed the command correctly, before pressing Enter.
Safer version
rm -R -i lampp
Adding -i
makes it a little safer, because it will prompt you on every deletion. However, if you are deleting many files this is not going to be very practical. Still, you can try this first.
Note about -f
option:
Many people suggest using -f
(combining it into -Rf
or -rf
), claiming that it gets rid of annoying prompts. However, in normal cases you don't need it, and using it suppresses some problems that you probably do want to know about. When you use it, you won't be warned if your arguments supply a non-existing directory or file(s): rm will just silently fail to delete anything. As a general rule, try first without the -f
: if there are problems with your arguments, then you'll notice. If you start getting too many prompts about files without write access, then you can try it with -f
. Alternatively, run the command from a user (or the superuser using sudo) that has full permissions to the files and directories you're deleting to prevent these prompts in the first place.