bash - 为什么 **find** 找不到任何东西?

标签 bash unix shell ksh

我正在寻找安装在我的系统上的 shell 脚本文件,但是find 不起作用:

$ find /usr -name *.sh

但我知道那里有大量的脚本。例如:

$ ls /usr/local/lib/*.sh
/usr/local/lib/tclConfig.sh  
/usr/local/lib/tkConfig.sh

为什么查找不起作用?

最佳答案

尝试引用通配符:

$ find /usr -name \*.sh

或:

$ find /usr -name '*.sh'

如果您碰巧在当前工作目录中有一个匹配*.sh 的文件,通配符将在find 发现它之前展开。如果您的工作目录中碰巧有一个名为 tkConfig.sh 的文件,find 命令将扩展为:

$ find /usr -name tkConfig.sh

这只会找到名为 tkConfig.sh 的文件。如果你有多个文件匹配 *.sh,你会从 find 得到一个语法错误:

$ cd /usr/local/lib
$ find /usr -name *.sh
find: bad option tkConfig.sh
find: path-list predicate-list

同样,原因是通配符扩展到两个文件:

$ find /usr -name tclConfig.sh tkConfig.sh

引用通配符可防止其过早展开。

另一种可能性是/usr 或其子目录之一是符号链接(symbolic link)。 find 通常不会跟随链接,因此您可能需要 -follow 选项:

$ find /usr -follow -name '*.sh'

关于bash - 为什么 **find** 找不到任何东西?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18836/

相关文章:

linux - Bash - 如何在这种情况下调用函数

Linux:从文件名中获取特定字段

python - 从 bash 脚本调用时获取 Python 命令行输出

linux - 通过 SSH 通过 STDIN 将指示器从 Bash 传回 Perl

unix - 如何使用unix “find”命令查找所有cpp和h文件?

bash - 访问 bash 函数中的命令行参数?

linux - 我怎样才能执行有输入的命令

regex - 将正则表达式匹配绑定(bind)到bash中的变量

c++ - “rusage” 统计

bash - 如何将任意参数传递给通过SSH执行的命令?