在以破折号开头的目录中查找 [英] find in directory that starts with dash

查看:28
本文介绍了在以破折号开头的目录中查找的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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?

一种解决方案是找到$(readlink -f -- "$test_filename")",但这很难看.有更好的想法吗?

One solution is to find "$(readlink -f -- "$test_filename")", but it's ugly. Any better ideas?

编辑 2:感谢您的建议.以下是这项工作产生的两个脚本:safe-find.shsafe-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,

第一个以 a 开头的参数'-' (...) 和所有后续参数应被解释为表达式

The first argument that starts with a '-' (...) and all subsequent arguments shall be interpreted as an expression

所以 -- 确实行不通.thkala 的解决方案也可能有效,但我发现它的可读性较差.不过,如果您执行大量 find 调用,它可能会更快.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆