18comments Written By: Scott Jangro
June 12, 2007

Unix: How to Delete Files That Start with Special Characters

If you're new here, please subscribe to my RSS feed. And follow me on twitter. Thanks for visiting!

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#

18 Responses to “Unix: How to Delete Files That Start with Special Characters”

  1. 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.

  2. Scott thanks much for posting the solution for removing this annoyingly named file, it worked like a charm. Great help!

  3. 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

  4. 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

  5. You can also delete files having special name in their filenames with inode number.
    Ex:
    > ls -li ==> first column is inode num
    > find . -inum -exec rm {} ;

    thats it.

    thanks

  6. Thankyou so much. This works.

  7. Thanks above solution is really useful.

  8. thanks for the easy solution

  9. I found uot that you can delete a file starting with hyphen by following command.

    $rm — -filename

    Try this

  10. But I wonder how to remove a directory stating with hyphen. Can any one help!

  11. Very helpful!

  12. great tip! this worked for me!

  13. AWESOME!

    thanks! ive been looking for a solution to this

  14. 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.

  15. hey thanks for the information man! It was really useful.

    My friends were always asking about this.

  16. This was excellent!!! Finally, right to the point! Well written and well done. Thank you very much.

  17. Wonderful post. I was breaking my head how to delete files which had - in the begining. Great help Thx.

  18. kudos to u dude!!!!!!!!!!
    such a simple logic yet so tough
    awesome

Leave a Reply