rm cannot delete files starting with “--” [duplicate]

房东的猫 提交于 2019-12-09 13:09:01

问题


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

回答1:


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



回答2:


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



回答3:


Try file name with path:

$ rm ./--file.name

Example:

$ echo dgag > --test.txt
$ ls
--test.txt
$ rm ./--test.txt
$ ls


来源:https://stackoverflow.com/questions/13917714/rm-cannot-delete-files-starting-with

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!