我有一个小问题,我无法理解查找命令。
我能做到:
[root@hostnaoem# ❯❯❯ls /proc/*/fd
但这给了我一个错误:
[root@hostnaoem# ❯❯❯ find /proc/*/fd -ls
find: `/proc/*/fd': No such file or directory
即使我使用 "/proc//fd", /proc/" "/fd 或 "/proc/*/fd"
我搜索了 wha find shell expansion says about that,但我一无所获。谁能告诉我为什么?
谢谢
最佳答案
如果你只是 RTFM,你会了解到 find 的语法是:
find [-H] [-L] [-P] [-D debugopts] [-Olevel] [path...] [expression]
通常使用的子集是:
find whereToSearch (-howToSearch arg)*
在/proc
中查找所有名为fd
的文件|目录:
find /proc -name fd
-name
是最常见的 howToSearch 表达式:
-name pattern
Base of file name (the path with the leading directories removed) matches shell pattern pattern. The metacharacters (`*', `?', and `[]') match a `.' at the start of the base name (this is a change in findutils-4.2.2; see section STANDARDS CON‐ FORMANCE below). To ignore a directory and the files under it, use -prune; see an example in the description of -path. Braces are not recognised as being special, despite the fact that some shells including Bash imbue braces with a special meaning in shell patterns. The filename matching is performed with the use of the fnmatch(3) library function. Don't forget to enclose the pattern in quotes in order to protect it from expansion by the shell.
(注意最后一句)
如果您的模式包含斜线,您需要 -path
或 -wholename
(相同的东西):
find /proc/ -wholename '/proc/[0-9]*/fd' 2>/dev/null
您可能想使用的其他表达方式是: -类型 -深度,-mindepth,-maxdepth -用户,-uid
参见 find(1)了解有关每个搜索表达式的更多信息。如果要搜索终端手册(man find
或man 1 find
),可以使用/
字符进入搜索模式(类似于大多数 GUI 应用程序中的 Ctrl+F
)。
ls
与 globbing (*
) 的结合使用通常是一种代码味道。除非您使用 -d
标志,否则除了匹配之外,它还会列出与 glob 模式匹配的目录的内容。
我发现 echo globpattern
形式通常更便于查看 glob 模式匹配的结果。
关于Linux 查找命令 shell 扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31938250/