rm无法删除以“ - ”开头的文件[复制]

问题描述 投票:5回答:3

这个问题在这里已有答案:

我有一个创建文件的脚本,有时它们最初会有两个破折号。有没有办法删除它们? mv也不起作用。

这是我得到的错误:

$ 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
linux bash shell rm
3个回答
19
投票

通常的伎俩是

rm ./--1355509766.jpg

更新:这是man rm对此的评价:

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

       rm -- -foo

       rm ./-foo

9
投票

使用--将选项与参数分开。

$ rm -- --1355509766.jpg

这也适用于其他命令。例如:

$ touch -- -v         # create a file called -v
$ grep foo -- -v      # grep for "foo" in file called -v

6
投票

尝试使用路径的文件名:

$ rm ./--file.name

例:

$ echo dgag > --test.txt
$ ls
--test.txt
$ rm ./--test.txt
$ ls
© www.soinside.com 2019 - 2024. All rights reserved.