Getting rid of a file called "-d"

18

2

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?

phileas fogg

Posted 2011-09-08T20:24:01.913

Reputation: 303

Question was closed 2011-09-08T23:44:53.273

Answers

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.

Matteo Riva

Posted 2011-09-08T20:24:01.913

Reputation: 8 341

28

rm ./-d

is the answer to your question.

bmargulies

Posted 2011-09-08T20:24:01.913

Reputation: 1 121

1rm -- -d works also – Ulrich Dangel – 2011-09-08T20:28:06.167

This is the best solution – gd1 – 2011-09-08T21:03:46.407

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

che2cbs

Posted 2011-09-08T20:24:01.913

Reputation: 181

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

user unknown

Posted 2011-09-08T20:24:01.913

Reputation: 1 623