Unix: How to Delete Files That Start with Special Characters

posted by jangro on (4 years, 11 months ago)

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#


Comments & Reactions

  • jangro saved this to Linux 4 years, 11 months ago
  • Posted by Dubai news 4 years, 11 months ago

    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.

  • Posted by Herb 4 years, 10 months ago

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

  • Posted by Ken 4 years, 9 months ago

    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

  • Posted by Ken 4 years, 9 months ago

    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

  • Posted by Jagadeesh 4 years, 9 months ago

    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

  • Posted by amit shanbhag 4 years, 9 months ago

    Thankyou so much. This works.

  • Posted by Abhinav 4 years, 9 months ago

    Thanks above solution is really useful.

  • Posted by vicky 4 years, 8 months ago

    thanks for the easy solution

  • Posted by Sat 4 years, 2 months ago

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

    $rm -- -filename

    Try this

  • Posted by Sat 4 years, 2 months ago

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

  • Posted by Mark 4 years ago

    Very helpful!

  • Posted by flamrick 3 years, 9 months ago

    great tip! this worked for me!

  • Posted by clarKent 3 years, 9 months ago

    AWESOME!

    thanks! ive been looking for a solution to this

  • Posted by Tech Forum 3 years, 8 months ago

    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.

  • Posted by GadgetAdvisor 3 years, 5 months ago

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

    My friends were always asking about this.

  • Posted by Star! 3 years, 4 months ago

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

  • Posted by Ravi 3 years, 1 month ago

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

  • Posted by nitesh 2 years, 11 months ago

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

  • Posted by Blackmercury 2 years, 10 months ago

    okay i need UNIX commmands to delete logs.txt and track logs.php and Parent Directory, for a challenge on hellboundhackers, tips or not???

  • Posted by Ken 2 years, 6 months ago

    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

  • Posted by prabhak 2 years, 5 months ago

    Wow!!!!!! That was wonderful and simple. THanks buddy.

  • Posted by Andy 1 year, 12 months ago

    very neat tip you got here! as I have 3-4 files with wildcard on it!

    thanks again


Leave a comment


Comment via Facebook

Listed Under

Reactions

Tags
    This item hasn't been tagged.