5

I have a script that creates files, and sometimes they end up having two dashes at the beginning. Is there any way to delete them? mv doesn't work either.

Here is the error I am getting:

$ ls
 --1355509766.jpg

$ rm --1355509766.jpg 
rm: illegal option -- -
usage: rm [-f | -i] [-dPRrvW] file ...
   unlink file

$ rm "--1355509766.jpg"
rm: illegal option -- -
usage: rm [-f | -i] [-dPRrvW] file ...
   unlink file
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
frazras
  • 5,928
  • 4
  • 30
  • 40

3 Answers3

18

The usual trick is

rm ./--1355509766.jpg

Update: here's what man rm has to say about this:

To  remove a file whose name starts with a '-', for example '-foo', use
one of these commands:

       rm -- -foo

       rm ./-foo
Lev Levitsky
  • 63,701
  • 20
  • 147
  • 175
9

Use -- to separate options from parameters.

$ rm -- --1355509766.jpg

This works with other commands too. For example:

$ touch -- -v         # create a file called -v
$ grep foo -- -v      # grep for "foo" in file called -v
dogbane
  • 266,786
  • 75
  • 396
  • 414
6

Try file name with path:

$ rm ./--file.name

Example:

$ echo dgag > --test.txt
$ ls
--test.txt
$ rm ./--test.txt
$ ls
Rasim
  • 1,276
  • 1
  • 11
  • 24
  • Yes , this worked great ....I had a file name that start with the hash sign #file_name# and this was how i deleted it rm ./#file_name# – z atef Jul 30 '14 at 14:36