18

Possible Duplicates:
How do I delete a file named "-p" from bash?
How to delete file with this name on linux: -]???????q
What command do I need to use to remove a file called `-rf`?

I've accidentally created a file called: -d

I've tried using single and double quote marks as well as wild cards to remove it but every time "rm" gives me this error:

Warning: --directory (-d) option is undocumented and no-op. Use -rf for deleting non-empty dirs rm: missing operand Try `rm --help' for more information.

How do I get rid of the file?

0

4 Answers 4

44
rm -- -d

-- means "end of options". Anything further on the command line following this is interpreted as an argument (i.e. the file name in your case), and not an option.

0
28
rm ./-d

is the answer to your question.

2
  • 1
    rm -- -d works also Sep 8, 2011 at 20:28
  • This is the best solution
    – gd1
    Sep 8, 2011 at 21:03
8

Using '--' is by far the easiest in this specific case. However, a more general solution if you stumble across a file with unprintable control characters is to reference the file by inode:

% ls -ali aFileWithFunnyCharacters
      9215 -rw-r-----   1 chris  chris         0 Sep  8 16:55 aFileWithFunnyCharacters
% find . -xdev -inum 9215 -exec rm {} \;
% ls -ali aFileWithFunnyCharacters
aFileWithFunnyCharacters: No such file or directory
0

Gnu-find has a -delete option:

find -name "-d" -delete

else you could try

find -name "-d" -exec rm {} ";" 

but the -- -solution from above is shorter, and the way to stop interpretation of flags with -- is used by many programs, using the getopt-library, so learning it is a useful thing in general.

But find/-delete is something very useful to learn too. Note that both examples will delete files of name "-d" recursively, which might not happen often, but if you happen to have one file, named "-d", you might have more of them. :)

Not the answer you're looking for? Browse other questions tagged .