Deleting a UNIX directory with a hyphen in the name

Solution 1:

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 ..

Solution 2:

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"


Solution 3:

Rename it and then deal with it normally:

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

Solution 4:

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.