我正在寻找安装在我的系统上的shell脚本文件,但是find命令不起作用:
$ find /usr -name *.sh
但我知道有很多脚本可用,例如:
$ ls /usr/local/lib/*.sh
/usr/local/lib/tclConfig.sh
/usr/local/lib/tkConfig.sh
为什么find不起作用?试着引用通配符:
$ 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'
.sh
扩展名怎么办?也许可以使用类似于 find /xyz | xargs file | grep shell
的命令? - Paul Ratazzi.sh
的脚本。 - Jan在某些系统上(例如 Solaris),没有默认操作,因此您需要添加 -print 命令。
find /usr -name '*.foo' -print
在寻找硬盘上的文件时,建议使用“locate”命令,它可以即时查找(检索每日构建的索引)。 例如:
locate '/usr*.sh'
mlocate
软件包 - 别忘了之后运行sudo updatedb
命令来构建索引... - SlySven