UNIX – How to Delete Directory with Hyphen in Name

unixunix-shell

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?

Best Answer

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