17

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

$ find /usr -name *.sh

但我知道那里有很多脚本。例如:

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

为什么找不到工作?

4

3 回答 3

54

尝试引用通配符:

$ 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 或其子目录之一是符号链接。 find通常不跟随链接,因此您可能需要-follow选项:

$ find /usr -follow -name '*.sh'
于 2008-08-20T21:04:01.003 回答
15

在某些系统(例如 Solaris)上,没有默认操作,因此您需要添加 -print 命令。

find /usr -name '*.foo' -print
于 2008-08-20T21:05:08.847 回答
8

为了在磁盘上查找文件,倾向于使用“定位”而不是即时的(查看每日构建的索引),您的示例将是:

locate '/usr*.sh'
于 2009-01-24T23:28:27.497 回答