为什么**找不到**?

Jon*_*son 17 unix bash shell ksh

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

$ find /usr -name *.sh
Run Code Online (Sandbox Code Playgroud)

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

$ ls /usr/local/lib/*.sh
/usr/local/lib/tclConfig.sh  
/usr/local/lib/tkConfig.sh
Run Code Online (Sandbox Code Playgroud)

为什么找不到工作?

Jon*_*son 52

尝试引用通配符:

$ find /usr -name \*.sh
Run Code Online (Sandbox Code Playgroud)

要么:

$ find /usr -name '*.sh'
Run Code Online (Sandbox Code Playgroud)

如果您碰巧在当前工作目录中有一个与*.sh匹配的文件,则会在查看之前扩展通配符.如果您的工作目录中碰巧有一个名为tkConfig.sh的文件,则find命令将扩展为:

$ find /usr -name tkConfig.sh
Run Code Online (Sandbox Code Playgroud)

只能找到名为tkConfig.sh的文件.如果您有多个匹配*.sh的文件,则会从find中收到语法错误:

$ cd /usr/local/lib
$ find /usr -name *.sh
find: bad option tkConfig.sh
find: path-list predicate-list
Run Code Online (Sandbox Code Playgroud)

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

$ find /usr -name tclConfig.sh tkConfig.sh
Run Code Online (Sandbox Code Playgroud)

引用通配符可防止它过早扩展.

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

$ find /usr -follow -name '*.sh'
Run Code Online (Sandbox Code Playgroud)


Mar*_*son 15

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

find /usr -name '*.foo' -print
Run Code Online (Sandbox Code Playgroud)


Col*_*boo 7

要查找磁盘上的文件,请使用"locate"来代替即时(查看每日构建的索引),例如:

locate '/usr*.sh'
Run Code Online (Sandbox Code Playgroud)