Thursday, June 18, 2009

Remove a file with a dash as first character or with special characters

Recently came across a file with dash in front and couldn't able to delete it with simple rm or rm with quotes.

Special characters files can be deleted using, if you know or can see all the special characters. For example pankaj%76$

rm "pankaj%76$"

However if there is a file like -pankaj%76$ (dash in front of it). Use
rm ./-pankaj%76$
or
rm -- -pankaj%76$

Using ./ or -- prevents the dash from coming in front of the filename and also being interpreted as rm command option.