Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rm cannot delete files starting with "--" [duplicate]

Tags:

linux

bash

shell

rm

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
like image 222
frazras Avatar asked Dec 17 '12 16:12

frazras


3 Answers

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
like image 172
Lev Levitsky Avatar answered Oct 05 '22 06:10

Lev Levitsky


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
like image 43
dogbane Avatar answered Oct 05 '22 05:10

dogbane


Try file name with path:

$ rm ./--file.name

Example:

$ echo dgag > --test.txt
$ ls
--test.txt
$ rm ./--test.txt
$ ls
like image 38
Rasim Avatar answered Oct 05 '22 06:10

Rasim