Unix: How to Delete Files That Start with Special Characters

by Scott Jangro on 12 June 2007

linux.jpgCame across this neat trick today. I find that if I don’t write about them somewhere, I forget and have to hunt around next time it happens. So why not here.

I found a file that was created accidentally that was named starting with hyphens, like –filename.

You cannot simply type rm –filename as the command line parser treats the filename as a command line switch. There are other characters that work the same way, as I discovered, like #, which gets created by some text editors as a backup file, e.g., #myfile#.

A simple way to delete these files is to find another way to access them so the special character isn’t the first character after the whitespace. This can be done by referencing them via their path location, or most simply by preceding the filename with ./ which is the current directory.

The files in the above examples can be deleted with the following commands:

$ rm ./--filename

$rm ./#myfile#

  • Blackmercury
    okay i need UNIX commmands to delete logs.txt and track logs.php and Parent Directory, for a challenge on hellboundhackers, tips or not???
  • nitesh
    kudos to u dude!!!!!!!!!!
    such a simple logic yet so tough
    awesome
  • Ravi
    Wonderful post. I was breaking my head how to delete files which had - in the begining. Great help Thx.
  • Star!
    This was excellent!!! Finally, right to the point! Well written and well done. Thank you very much.
  • hey thanks for the information man! It was really useful.

    My friends were always asking about this.
  • Good one! I think that will settle the matter with some of the friends living in my hardware and smirking at me.

    Will try it out and tell you how the funeral went.
  • Ken
    Hi Scott,

    Thanks for the post. I renamed a file with a - in front, not knowing how i could copy nor remove it, until i find this link !

    Cheers
  • now that is a "too easy solution"
    remove from this folder this -file

    if you need it you will never think of it, also don't think that would be easy to google an answer.

    nothing harder than finding the easy solution to your problem at hand.
blog comments powered by Disqus