Deleting a UNIX directory with a hyphen in the name

7,265

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.

Solution 5

rm -fr ./-A

The above command works for me.

Share:
7,265

Related videos on Youtube

José Nobile
Author by

José Nobile

Software developer but interested in a great many other things.

Updated on September 17, 2022

Comments

  • José Nobile
    José Nobile almost 2 years

    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?

    • user1686
      user1686 almost 15 years
      Luckily the directory wasn't named '-rf *'.
    • Dmitry Volkov
      Dmitry Volkov about 7 years
      Same on stackoverflow and unix.
  • chris
    chris almost 15 years
    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.
  • baumgart
    baumgart almost 15 years
    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.
  • dave_thompson_085
    dave_thompson_085 almost 5 years
    Nit: -exec doesn't escape the name, and doesn't need to; the argument passed via one of the exec* syscalls is the actual name. It is only shell input that needs special chars like space semicolon asterisk etc. quoted to get shell to pass the correct (WITHOUT quotes) actual name. That said, find -exec rm -f {} only works for a file, not a directory as asked for this Q; find -exec rm -rf {} does handle directory and even nonempty directory, but skipping find and just doing rm -rf ./-A is much simpler.
  • dave_thompson_085
    dave_thompson_085 almost 5 years
    This is nearly duplicate of baumgart's answer from 10 years ago, but you don't need rm -d ever and usually not -f when invoking from find, but you do need -r for a directory and don't need it for a file. Even when corrected this method is still clumsy and inferior. For a nonempty directory just do rm -fr ./-p -- that's r for a directory, and f depending on the permission settings.
  • RalfFriedl
    RalfFriedl almost 5 years
    Both methods were already mentioned in previous answers.