Linux 查找命令 shell 扩展

标签 linux sh glob

我有一个小问题,我无法理解查找命令。

我能做到:

[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 findman 1 find),可以使用/ 字符进入搜索模式(类似于大多数 GUI 应用程序中的 Ctrl+F)。


ls 与 globbing (*) 的结合使用通常是一种代码味道。除非您使用 -d 标志,否则除了匹配之外,它还会列出与 glob 模式匹配的目录的内容。 我发现 echo globpattern 形式通常更便于查看 glob 模式匹配的结果。

关于Linux 查找命令 shell 扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31938250/

相关文章:

python - 如何使用glob打印到python中文件的路径

linux - unrar : error while loading shared libraries: libstdc++. so.6:无法打开共享对象文件:没有这样的文件或目录

Linux : Copy Multiple files and remove it's extension

linux - 我怎样才能删除字符串并使用它前面的数字

linux - 在 jenkinsfile 中使用命令 sh 的正确语法

linux - 我如何创建一个通用的 shell 命令来退出或从执行或获取的脚本中返回?

linux - 从文件中提取版本

linux - find命令+根据目录时间戳删除旧目录

python - 如何在 python 中将字符串数据帧转换为 csv?

perl - 为什么在标量上下文中Perl文件glob()无法在循环外工作?