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.

1 comment:

Pankaj Gautam said...

also
ls -li will give you inode for that file. Once you have the inode number of that file.

Assuming 294673 is the inode number
find . -inum 294673 -exec rm -i {} \;