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!
Came 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#




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.
Scott thanks much for posting the solution for removing this annoyingly named file, it worked like a charm. Great help!
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
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
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
Thankyou so much. This works.
Thanks above solution is really useful.
thanks for the easy solution
I found uot that you can delete a file starting with hyphen by following command.
$rm — -filename
Try this
But I wonder how to remove a directory stating with hyphen. Can any one help!
Very helpful!
great tip! this worked for me!
AWESOME!
thanks! ive been looking for a solution to 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.
hey thanks for the information man! It was really useful.
My friends were always asking about this.
This was excellent!!! Finally, right to the point! Well written and well done. Thank you very much.
Wonderful post. I was breaking my head how to delete files which had - in the begining. Great help Thx.
kudos to u dude!!!!!!!!!!
such a simple logic yet so tough
awesome