找到以破折号开头的目录 [英] find in directory that starts with dash
问题描述
find
将文件名开头的破折号解释为选项的开头。使用熟悉的 -
技巧不起作用,因为选项在文件名后,引用没有任何效果,并用 \-替换第一个虚线,
也无效。经常鼓励用户使用 ./
之前的这些文件名,但如果不知道给定的路径是绝对还是相对的,我该怎么办?
find
interprets a dash at the start of a filename as the start of an option. Using the familiar --
trick doesn't work since options are after the filename, quoting has no effect, and replacing the first dash with \-
doesn't work either. Users are often encouraged to precede such filenames with ./
, but what can I do if I don't know whether the given path will be absolute or relative?
编辑:一个解决方案是 find$(readlink -f - $ test_filename)
,但它是丑陋的。任何更好的想法?
One solution is to find "$(readlink -f -- "$test_filename")"
, but it's ugly. Any better ideas?
编辑2 :感谢您的建议。以下是这项工作产生的两个脚本: safe-find.sh ; safe-count-files.sh
Edit 2: Thanks for the suggestions. Here are the two scripts that resulted from this effort: safe-find.sh; safe-count-files.sh
推荐答案
这可能看起来有点便宜,但实际上我推荐你已经想出的 readlink
解决方法。根据 Unix标准,
This may seem a bit cheap, but I actually recommend the readlink
workaround that you've figured out. According to the Unix standard,
以
' - '(...)开头的所有后续参数
的第一个参数将被解释为
表达式
所以 -
。 thkala的解决方案也可以正常工作,但是我发现它的可读性差。可能会更快,但如果您正在做很多查找
调用。
so --
will indeed not work. thkala's solution may also work, but I find it less readable. It may be faster though, if you're doing a lot of find
invocations.
这篇关于找到以破折号开头的目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!