rm -rf doesn't work for an empty directory
Solution 1
I reckon you might have a file in the foo
directory with unprintable characters in it's name. Compare the characters you see in the ls
output with the actual characters ls
outputs.
cd foo
ls # you see what your terminal lets you see
ls | od -a # you see the character codes *really* coming from ls
There are various methods to help delete a file whose name you can't easily see or type.
Here you could use the interactive -i
option of rm.
cd foo
rm -i *
Obviously, be careful with this. And only say y
to the one you want to delete.
As to why your first rm -rf
didn't delete it... I wonder if you have rm aliased? Use alias rm
to see. You can temporarily run the real version of rm (bypassing the alias) using \rm -rf foo
.
Solution 2
The "Directory not empty" message is quite misleading. Normally, an rm -rf
will remove everything in a directory, recursively, so it wouldn't matter if it's empty or not.
In this case, there are some things you might want to check:
- Try seeing if there's anything mounted in this directory with
df -h
, and unmount if necessary - Try checking if there's a file open by an application, running
sudo lsof foo
, and quit the application(s) if necessary - Try
sudo rm -rf foo
– perhaps you just don't have permissions (although I don't think that's the case here) - Try logging out and back in
- Try rebooting the machine
Related videos on Youtube
guest0105
Updated on September 18, 2022Comments
-
guest0105 over 1 year
In terminal I try to delete a directory but that doesn't work:
myuser$ rm -rf foo/ rm: foo/: Directory not empty
In my main directory I have only foo :
myuser$ ls foo
I haven't any problem with my 'ls -la' command:
myuser$ ls -la drwxrwxrwx@ 1 myuser staff 65536 1 mai 10:53 . drwxrwxrwx@ 1 myuser staff 32768 28 aoû 2013 .. drwxrwxrwx 1 myuser staff 32768 1 mai 10:36 foo
Directory foo seems to be empty :
myuser$ ls foo/ myuser$ ls -la foo/ ls: : No such file or directory total 192 drwxrwxrwx 1 myuser staff 32768 1 mai 10:36 . drwxrwxrwx@ 1 myuser staff 65536 1 mai 10:53 ..
But the line "ls: : No such file or directory" is weird. And I think it's the reason I can't delete this directory. We could see too, that "ls foo" return an empty line, like there is something, but what? And how delete it?
Thanks
-
Jozef Legény about 10 yearsCan you see the folder in the GUI? Can you cd into it?
-
slhck about 10 yearsIs that directory a mount point for something? Check with
df -h
. What's the output oflsof foo
? Have you rebooted yet? Are there any encrypted files? -
Daniel B about 10 yearsYou could also run a filesystem check from Disk Utility.
-
Oliver Salzburg about 10 yearsThis question appears to be off-topic because the user no longer has the issue
-
Lqueryvg about 10 years@Oliver, how do you know the user no longer has the issue ? They haven't responded yet, and the original question/problem seems real enough; worthy of a question and answer surely ?
-
Oliver Salzburg about 10 years@Lqueryvg: He posted an answer (which should have been a comment or an edit), which was flagged and deleted.
-
kenorb about 9 years
-
-
slhck about 10 yearsThat's wrong.
rm -rf dir
does remove hidden files in that directory. Try runningmkdir test && touch test/.foo && rm -rf test
as proof. The "directory not empty" error is a little misleading. -
MariusMatutiae about 10 yearsSorry, @slhck, but this time you are wrong. Try: mkdir ttp; cd ttp; touch .test; ls -a; rm -rf *; ls -a and you will discover that .test is still there. You owe me 2 points, thank you.
-
slhck about 10 yearsYour example shows something different – namely that the
*
glob does not include hidden files. Which is of course correct, but irrelevant to the question being asked, since there's no globbing involved. You said that "rm -rf dir
will not remove hidden files", which is wrong, and which is what my example shows.rm -rf dir
will delete all directory contents, regardless of whether they're dotfiles or not.