13

Through a boneheaded maneuver on my part, I accidentally created a directory called (for instance) -A, and ended up filling it with files.

I want to delete said directory. I've tried:

rmdir -- -A

but it then tells me that the directory still has files in it. And I can't figure out how to cd into the directory to delete said files.

What should I do to get rid of this troublesome directory?

29

Use -- on every command.

$ ls -la
total 32
drwxr-xr-x  3 richard  richard  512 Jul 28 15:44 .
drwxr-xr-x  3 root     wheel    512 Jul  6 17:10 ..
$ mkdir -- -A
$ ls -la
total 36
drwxr-xr-x  2 richard  richard  512 Jul 28 15:44 -A
drwxr-xr-x  4 richard  richard  512 Jul 28 15:44 .
drwxr-xr-x  3 root     wheel    512 Jul  6 17:10 ..
$ cd -- -A
$ ls
$ pwd
/home/richard/-A
$ cd ..
$ rm -rf -- -A
$ ls -la
total 32
drwxr-xr-x  3 richard  richard  512 Jul 28 15:44 .
drwxr-xr-x  3 root     wheel    512 Jul  6 17:10 ..
11

You can put ./ in front of the file -- that way rm or rmdir won't behave as though the filename is an option flag.

You can also issue the option -- which tells rm to act as though everything after the -- is a filename and that it should not process any more options. There may be funky older versions of rm that don't obey that, though my zoo of antique unixes has gotten pretty small these days so I can't tell you which ones or if there are versions that don't understand --.

You should get into the habit of putting the ./ in front of names when you're deleting anyhow -- you never know if there is a -r or -rf named file in your directory. You could say that you should always use the -- but I find that the ./ is more natural because it makes explicit that "I want to delete files in this directory, not whatever * happens to glob out to"

4

Rename it and then deal with it normally:

$ mv -- -A foo
$ find foo
$ rm -rf foo
2

rmdir will not delete a directory with anything inside it. "rm -rf" will. rmdir is considerably safer than "rm -rf". Moo's answer is still probably the best.

1
rm -fr ./-A

The above command works for me.

0

A different method would be to delete by inode (this works for files/directories with other characters that are difficult to delete with just rm ).

bash-3.2$ ls -i
4905937 angry-1.gif
5174052 hermes
5172770 make_hosts.pl
bash-3.2$ find . -inum 5172770 -exec rm -f {} \;
bash-3.2$ ls -i
4905937 angry-1.gif
5174052 hermes
bash-3.2$
  • the rm will still be passed a -A argument on the commandline and will still try to interpret it as an option, so you still need to prevent it from seeing the leading dash somehow (through double dashes or by putting ./ in front of the rest of the name. – chris Jul 28 '09 at 19:10
  • The find will prepend that automatically, since you're finding from '.'. The filename passed in will be './-A', or whatever the file might be called, prepended with ./ and properly escaped for the command. – baumgart Jul 29 '09 at 17:07
0

find can delete the directory in this way:

find . -type d -iname '-A' -delete

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.