# rm -rf doesn't work for an empty directory [closed]

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

-

## closed as off-topic by Oliver Salzburg♦May 2 at 13:59

• This question does not appear to be about computer software or computer hardware within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

Can you see the folder in the GUI? Can you cd into it? –  Jozef Legény May 1 at 9:36
Is that directory a mount point for something? Check with df -h. What's the output of lsof foo? Have you rebooted yet? Are there any encrypted files? –  slhck May 1 at 10:09
You could also run a filesystem check from Disk Utility. –  Daniel B May 1 at 10:21
This question appears to be off-topic because the user no longer has the issue –  Oliver Salzburg May 2 at 13:59
@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 ? –  Lqueryvg May 2 at 15:40

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.

-

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
-

The command

 rm -rf dir


does not remove hidden files, i.e. those starting with a dot, like for instance .bashrc. The directory not empty diagnostic means you have some some hidden files, you may list them with either

 ls -a


or ith

 ls .*


You can erase them recursively with

 rm -rf .[a-Z]*


then you will be able to rmdir the offending directory.

EDIT:

The following Edit proves my point:

  root@rasal:/tmp# mkdir ttp
root@rasal:/tmp# cd ttp
root@rasal:/tmp/ttp# touch .test
root@rasal:/tmp/ttp# ls -a
.  ..  .test
root@rasal:/tmp/ttp# rm -rf *
root@rasal:/tmp/ttp# ls -a
.  ..  .test
root@rasal:/tmp/ttp#

-
That's wrong. rm -rf dir does remove hidden files in that directory. Try running mkdir test && touch test/.foo && rm -rf test as proof. The "directory not empty" error is a little misleading. –  slhck May 1 at 10:07
Sorry, @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. –  MariusMatutiae May 1 at 10:16
Your 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. –  slhck May 1 at 10:17