Monday 2 October 2017

linux - How to remove a file with name starting with "-r" using cli



I have been creating odd-named files every once in a while, e.g.


$ls -rthl
$-rw-r--r-- 1 shamil hep 290 Aug 13 11:58 -rf

And interestingly it is impossible to remove this file with


rm -f -rf

I know I need to escape the special symbols like "-", so the issue is clear. And I used to know the solution, but have since forgotten.


How do I properly delete it?


I have tried things like this


rm -f \-rf

but to no avail.



Answer



-- switch means: End of flags, everything after it is assumed to be a file name. So you can do:


rm -- -rf

Include the full path to the file also works:


rm /full/path/to/-rf

Actually, this is kind of hilarious but this command line can give you the answer :)


$man rm | cat | grep -B4 -A3 -- "rm --"

To remove a file whose name starts with a `-', for example `-foo', use
one of these commands:

rm -- -foo
rm ./-foo

No comments:

Post a Comment

Where does Skype save my contact's avatars in Linux?

I'm using Skype on Linux. Where can I find images cached by skype of my contact's avatars? Answer I wanted to get those Skype avat...