Oxaric’s Blog

A compendium of amazing things…

Posts Tagged ‘names’

Linux Trick To Remove Files With Bad Names

Posted by oxaric on November 12, 2008

Occasionally you might find you’ve created ‘bad’ file names. These file names start with a hyphen and are an annoyance and possibly dangerous to your system.  However, if you try to directly use ‘rm -file-name’ you’ll find it won’t work.  The solution to this problem is simple once you know how but can be very confusing if you don’t.

The ‘trick’ is to add “./” to the beginning of the file name before trying to rm the file. If the file name contains spaces you’ll also need to wrap the file name in double quotes.

For example:

~/test> ls
important-system-information

~/test> echo “This is a test” > -rf

~/test> ls
-rf
important-system-information

~/test> rm -rf

~/test> ls
-rf
important-system-information

~/test> rm “-rf

~/test> ls
-rf
important-system-information

~/test> rm “-rf” important-system-information/

~/test> ls
-rf

~/test> rm ./-rf

~/test> ls

~/test>



***POOF!***

The bad file name is gone! But unfortunately in this example we deleted our important system information directory by trying to simply wrap the bad file name in double quotes.  The rm command took ‘”-rf” as an option, which forcefully and recursively deletes, and we happened to type the name of our important system directory on the same line as the rm command.  OOPS!  Contrived example?  Yes.  But it works. :)

Posted in Linux, Tips | Tagged: , , , , , , | Leave a Comment »