How To Remove A File That Begins With A Hyphen '-'
BACKGROUND
In error, maybe due to a typo, the directory ends up containing a file that begins the a hyphen '-'. Other than removing the whole directory, removing a file with such a name can prove troublesome as Unix most command options are signified by the use of hyphen '-' too.
There is however a little documented Unix option that can actually negate, or switch off the use hyphens as option indicators and cause them to be treated as literal characters. It is the option hyphen, hyphen '—'.
EXAMPLE
cd mydir
ls
-typofile
goodfile1
goodfile2
rm -typofile
rm: illegal option -- t
rm: illegal option -- y
rm: illegal option -- p
rm: illegal option -- o
rm: illegal option -- l
rm: illegal option -- e
usage: rm [-fiRr] file ...
rm -- -typofile
ls
goodfile1
goodfile2
page revision: 9, last edited: 24 Aug 2011 07:49





