How do I get rid of a (Unix) file which has got a 'bad' name (e.g. starts with -)?

Use unlink(1), e.g.
unlink filename 
where filename is the name of the file you wish to remove.

Or you could try the following little script used by ACI:

#!/bin/bash
# rnbadfile
#
# v 1.0 ACI 14/5/03
#
# Use to delete a file with a bad file name
# eg one starting with a - sign
#
if [[ $# -ne 1 ]]
  then
  echo "Use rmbadfile to delete a file with a bad file name"
  echo "eg one starting with a - sign"
  echo "Usage:"
  echo "      $0 [mask]"
  echo "      (leave out a - if necessary)"
  exit 10
fi
echo Attempting to delete the file:
ls -li | grep $1 
read -p "....is this OK? (y/n): " ANS
#read ANS?"....is this OK? (y/n): "
if [ "$ANS" = "y" ]
then
 inode=`ls -li | grep $1 | cut -c1-10`
# echo $inode
 find . -inum $inode -exec mv \{\} tempname \;
 echo "file now renamed:"
 ls -l tempname
 rm tempname
 echo "file removed"
 ls -l tempname
 ls -l *$1*
else   
 echo "rmbadfile aborted"
fi


FAQ home page
Last modified: Mon Oct 27 11:15:18 2003