How to remove a directory which is not empty? [duplicate]
I've been trying to erase (remove) a directory from one of my internal HDD and the system tells me that "The directory can not be removed because it is not an empty directory."
The issue is that the directory IS empty!
At least, I can not to see any inside it with Thunar file manager or from the Terminal with root privileges.
The directory where the directory I want to delete is, has this output to ls-la:
total 28
drwxrwxrwx 1 root root 4096 ene 15 10:01 .
drwxrwxrwx 1 root root 20480 ene 15 10:01 ..
drwxrwxrwx 1 root root 4096 ene 15 10:01 matrimonio de fiesta sara y luis hernanAnd, the directory I want to delete, has this output:
total 8
drwxrwxrwx 1 root root 4096 ene 15 10:01 .
drwxrwxrwx 1 root root 4096 ene 15 10:01 ..A similar case with another directory which has 3 files and, each time I was trying to remove them, the system tells me: "Files not found."
I was trying with sudo rm -r -f and... Nothing!
The same history each time I used it. What's wrong here?
Is there another, more powerful, command to remove directories and/or files?
32 Answers
You can remove empty directories with rmdir <directory> and non-empty directories with rm -r <directory>.
What causes you trouble, may be the following:
Consider this directory content:
$ tree -a
.
├── empty
├── non-empty
│ └── file.txt
└── non-empty-with-hidden-file └── .file.txtWe have an epty directory, on containing a file and one containing a hidden file. Now run rmdir *:
$ rmdir *
rmdir: failed to remove `non-empty': Directory not empty
rmdir: failed to remove `non-empty-with-hidden-file': Directory not empty
$ tree -a
.
├── non-empty
│ └── file.txt
└── non-empty-with-hidden-file └── .file.txtAs you can see, empty was removed and the other two caused error messages. Now you can run rm -r:
$ rm -r non-empty
$ tree -a
.
└── non-empty-with-hidden-file └── .file.txtYour file not found error is probably not caused by rm, but by a typo in your filename or your directory path. You can avoid this by using the autocomplete function (Tab) instead of typing by hand.
Wayne's answer is correct, but if an error occurs you can add the '-f' switch to rm, "forcing" the removal:
sudo rm -rf "/path/to/the/directory"
More in general
"Zoraya ter Beek, age 29, just died by assisted suicide in the Netherlands. She was physically healthy, but psychologically depressed. It's an abomination that an entire society would actively facilitate, even encourage, someone ending their own life because they had no hope. Th…"