5

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

guest0105
  • 51
  • 1
  • 2

3 Answers3

2

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.

Lqueryvg
  • 608
1

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
slhck
  • 235,242
0

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# 
MariusMatutiae
  • 48,517
  • 12
  • 86
  • 136