7

After a mistake in a script I ended up with a file whose name starts with a dash, -:

-myfile.txt

I tried so far:

rm -myfile.txt
rm: illegal option -- m
usage: rm [-f | -i] [-dPRrvW] file ...
       unlink file

rm "-myfile.txt"
rm: illegal option -- m
usage: rm [-f | -i] [-dPRrvW] file ...
       unlink file

rm "\-myfile.txt"
rm: \-myfile.txt: No such file or directory

rm \-myfile.txt
rm: illegal option -- m
usage: rm [-f | -i] [-dPRrvW] file ...
       unlink file

rm "-"myfile.txt
rm: illegal option -- m
usage: rm [-f | -i] [-dPRrvW] file ...
       unlink file

How can I delete this file?

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
jrjc
  • 21,103
  • 9
  • 64
  • 78
  • 7
    __FAQ__: `rm -- filename` – devnull Feb 26 '14 at 17:03
  • 1
    Possible duplicates [here](https://stackoverflow.com/questions/13917714/rm-cannot-delete-files-starting-with) and [here](https://stackoverflow.com/questions/706196/how-to-remove-files-starting-with-double-hyphen) – ajp15243 Feb 26 '14 at 17:03
  • thanks @ajp15243. I didn't find it when searching – jrjc Feb 26 '14 at 17:05

4 Answers4

13

Thanks to @ajp15243 : the answer is :

rm ./-myfile.txt

or

rm -- -myfile.txt
jrjc
  • 21,103
  • 9
  • 64
  • 78
  • I think devnull deserves some credit for the `--` answer too :). You are encouraged to accept your answers to your own questions if that's what solved the problem. It will mark the question as solved for the next person who comes searching through SO for answers. – ajp15243 Feb 26 '14 at 17:08
  • Sure, but I actually understood what he meant thanks to your post ! (I can accept my answer in 2 days!) – jrjc Feb 26 '14 at 17:10
2

Use the absolute pathname

Like rm /home/name/-myfile.txt

Q_SaD
  • 355
  • 1
  • 11
1

you could always try the inode solution :

$ ls -al -i | grep me
2116530 -rw-rw-r--  1 user123 user123       0 Feb 27 12:39 me

and 2116530 is the inode of the file. Then you can use find to delete it

find ./ -inum 2116530 --delete

or even

find ./ -inum 2116530 -exec rm {\} \;
user2599522
  • 3,005
  • 2
  • 23
  • 40
0

try this one put your file name in "-" eg: rm "-myfile.txt"

Arun Binoy
  • 327
  • 1
  • 10