Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Possible Duplicates:
How do I delete a file named "-p" from bash?
How to delete file with this name on linux: -]???????q
Removing "-rf" file in Unix

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?

share|improve this question

migrated from stackoverflow.com Sep 8 '11 at 20:27

This question came from our site for professional and enthusiast programmers.

marked as duplicate by slhck, grawity, sblair, Gilles, Tom Wijsman Sep 8 '11 at 23:44

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

add comment

4 Answers

up vote 44 down vote accepted
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.

share|improve this answer
2  
Never knew about that trick. Thanks! –  phileas fogg Sep 8 '11 at 20:45
add comment
rm ./-d

is the answer to your question.

share|improve this answer
1  
rm -- -d works also –  Ulrich Dangel Sep 8 '11 at 20:28
    
This is the best solution –  gd1 Sep 8 '11 at 21:03
add comment

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
share|improve this answer
add comment

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

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.