bash 如何删除以双连字符开头的文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/706196/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to remove files starting with double hyphen?
提问by cb0
I have some files on my Unix machine that start with
我的 Unix 机器上有一些文件以
--
e.g. --testings.html
例如 --testings.html
If I try to remove it I get the following error:
如果我尝试删除它,我会收到以下错误:
cb0$ rm --testings.html
rm: illegal option -- -
usage: rm [-f | -i] [-dPRrvW] file ...
unlink file
I tried
我试过
rm "--testings.html" || rm '--testings.html'
but nothing works.
但没有任何效果。
How can I remove such files on terminal?
如何在终端上删除此类文件?
回答by Steve Jessop
rm -- --testings.html
The --option tells rm to treat all further arguments as file names, not as options, even if they start with -.
该--选项告诉 rm 将所有进一步的参数视为文件名,而不是选项,即使它们以-.
This isn't particular to the rm command. The getopt function implements it, and many (all?) UNIX-style commands treat it the same way: --terminates option processing, and anything after it is a regular argument.
这不是 rm 命令所特有的。getopt 函数实现了它,并且许多(所有?)UNIX 风格的命令都以同样的方式对待它:--终止选项处理,以及它之后的任何内容都是常规参数。
http://www.gnu.org/software/hello/manual/libc/Using-Getopt.html#Using-Getopt
http://www.gnu.org/software/hello/manual/libc/Using-Getopt.html#Using-Getopt
回答by lhunath
rm -- --somefile
While that works, it's a solution that relies on rmusing getopts for parsing its options. There are applications out there that do their own parsing and will puke on that too (because they might not necessarily implement the "--means end of options" logic).
虽然这rm可行,但它是一种依赖于使用 getopts 来解析其选项的解决方案。有一些应用程序会进行自己的解析,并且也会对此进行解析(因为它们可能不一定实现“--选项结束”逻辑)。
Because of that, the solution you should drive through your skull is this one:
因此,您应该通过头骨驱动的解决方案是:
rm ./--somefile
It will always work, because this way your arguments never begin with a -.
它总是有效的,因为这样你的论点永远不会以-.
Moreover, if you're trying to make really decent shell scripts; you should technically be putting ./in front of all your filename parameter expansions to prevent your scripts from breaking due to funky filename input (or to prevent them being abused/exploited to do things they're not supposed to do: for instance, rmwill delete files but skip over directories; while rm -rf *will delete everything. Passing a filename of "-rf" to a script or somebody touch ~victim/-rf'ing could in this way be used to change its behaviour with really bad consequences).
此外,如果您正在尝试制作真正体面的 shell 脚本;从技术上讲,您应该将./所有文件名参数扩展放在前面,以防止您的脚本由于时髦的文件名输入而中断(或防止它们被滥用/利用来做他们不应该做的事情:例如,rm将删除文件但跳过目录;whilerm -rf *将删除所有内容。将“ -rf”的文件名传递给脚本或某人touch ~victim/-rf可能会以这种方式用于更改其行为,并带来非常糟糕的后果)。
回答by Vatine
Either rm -- --testings.htmlor rm ./--testings.html.
无论是rm -- --testings.html或rm ./--testings.html。
回答by Juliano
rm -- --testings.html
回答by Juliano
Yet another way to do it is to use find ... -name "--*" -delete
另一种方法是使用 find ... -name "--*" -delete
touch -- --file
find -x . -mindepth 1 -maxdepth 1 -name "--*" -delete
回答by Dimitris
For a more generalised approach for deleting files with impossible characters in the filename, one option is to use the inode of the file.
对于删除文件名中包含不可能字符的文件的更通用方法,一种选择是使用文件的 inode。
It can be obtained via ls -i.
它可以通过 获得ls -i。
e.g.
例如
$ ls -lai | grep -i test
452998712 -rw-r--r-- 1 dim dim 6 2009-05-22 21:50 --testings.html
And to erase it, with the help of find:
并在 find 的帮助下擦除它:
$ find ./ -inum 452998712 -exec rm \{\} \;
This process can be beneficial when dealing with lots of files with filename peculiarities, as it can be easily scripted.
这个过程在处理大量具有文件名特性的文件时很有用,因为它可以很容易地编写脚本。
回答by Paul Smith
rm ./--testings.html
or
或者
rm -- --testings.html

![在 bash 中按 alt + numeric 你会得到 (arg [numeric]) 那是什么?](/res/img/loading.gif)