在以破折号开头的目录中查找
find
将文件名开头的破折号解释为选项的开头。使用熟悉的 --
技巧不起作用,因为选项位于文件名之后,引用无效,并且用 \-
替换第一个破折号也不起作用。通常鼓励用户在此类文件名前加上 ./
,但是如果我不知道给定路径是绝对路径还是相对路径,我该怎么办?
编辑:一种解决方案是查找“$(readlink -f -- "$test_filename")”
,但它很难看。还有更好的想法吗?
编辑2:感谢您的建议。以下是由此产生的两个脚本:safe-find.sh; safe-count-files.sh
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?
Edit: One solution is to find "$(readlink -f -- "$test_filename")"
, but it's ugly. Any better ideas?
Edit 2: Thanks for the suggestions. Here are the two scripts that resulted from this effort: safe-find.sh; safe-count-files.sh
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这可能看起来有点便宜,但我实际上推荐您已经找到的
readlink
解决方法。根据 Unix 标准,所以
--
确实不起作用。 thkala 的解决方案也可能有效,但我发现它的可读性较差。不过,如果您执行大量find
调用,它可能会更快。This may seem a bit cheap, but I actually recommend the
readlink
workaround that you've figured out. According to the Unix standard,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 offind
invocations.如果它在脚本中,您可以随时检查它。例如,对于 bash、ksh 或 zsh:
以更简洁的形式(对于 bash、ksh93 或 zsh):
您甚至可以使用脚本的参数来执行此操作,如果它们都应该是目录:
If it is in a script you can always check for it. E.g. for bash, ksh or zsh:
In a more terse form (for bash, ksh93 or zsh):
You can even do this with the parameters of a script, if they are all supposed to be directories:
这是一种适用于所有类 Unix 系统的方法,不需要特定的 shell 或非标准实用程序。
如果您的位置参数中有一个目录列表并想要处理它们,那么它会变得有点复杂。这是一个 POSIX sh 解决方案:
Bourne shell 和其他 POSIX sh 之前的实现缺乏算术和
set --
,所以它有点丑陋。1 <子>
readlink -f
可在 GNU(Linux、Cygwin 等)、NetBSD ≥4.0、OpenBSD ≥2.2、BusyBox 上使用。它在 Mac OS X(自 10.6.4 起)、HP-UX 上不可用(除非您已安装 GNU 工具,并且确保它们位于您的PATH
中) (自 11.22 起)、Solaris(自 OpenSolaris 200906 起)、AIX(自 7.1 起)。Here is a way that should work on all Unix-like systems, with no requirement on a specific shell or on a non-standard utility¹.
If you have a list of directories in your positional parameters and want to process them, it gets a little complicated. Here's a POSIX sh solution:
Bourne shells and other pre-POSIX sh implementations lack arithmetic and
set --
, so it's a little uglier.¹
readlink -f
is available on GNU (Linux, Cygwin, etc.), NetBSD ≥4.0, OpenBSD ≥2.2, BusyBox. It is not available (unless you've installed GNU tools, and you've made sure they're in yourPATH
) on Mac OS X (as of 10.6.4), HP-UX (as of 11.22), Solaris (as of OpenSolaris 200906), AIX (as of 7.1).使用 glob 捕获破折号:
edit:updated 以删除短语正则表达式,并引用 glob,以便它由 find 而不是 bash 解释(仅影响某些边缘情况)。
Use a glob to capture the dash:
edit: updated to remove the phrase regular expression, and to quote the glob so it's interpreted by find, not bash (only affects some edge cases).