【问题标题】:Why doesn't **find** find anything?为什么 **find** 找不到任何东西?
【发布时间】:2010-09-06 08:07:51
【问题描述】:

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

$ find /usr -name *.sh

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

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

为什么 find 不起作用?

【问题讨论】:

  • 这是超级用户的问题吗?
  • 这更适合UnixSE

标签: bash unix shell ksh


【解决方案1】:

要在磁盘上查找文件,请倾向于使用“定位”而不是即时的 (查看每日构建的索引) 你的例子是:

locate '/usr*.sh'

【讨论】:

  • 一个很好的建议。 (我必须在我的机器上运行 updatedb 来测试这个想法,因为我的 crontab 由于某种原因没有它。没有最新的数据库将是不使用 locate 的主要原因。)跨度>
  • 还有一些系统没有默认安装 locate - 我发现我必须在我的 Raspberry Pi 上安装 mlocate 包 - 不要忘记之后构建 sudo updatedb索引...
【解决方案2】:

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

find /usr -name '*.foo' -print

【讨论】:

  • 我过去一直被它所困扰。但我应该指出,在现代 Solaris 机器上, -print 默认是打开的。事实上,我似乎找不到一个不再以这种方式工作的发现。
【解决方案3】:

尝试引用通配符:

$ 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'

【讨论】:

  • 如果文件是 shell 脚本但缺少 .sh 扩展名怎么办?也许像find /xyz | xargs file | grep shell
  • 但是,shell 脚本 有 .sh 扩展名的频率如何?
  • 有什么通用方法可以确定通配符何时/何地被扩展?
  • 好吧,在 everything 有扩展名的 Windows 中长大,我在 *ix 上看到很多脚本都省略了 .sh
猜你喜欢
  • 2016-09-17
  • 2015-08-29
  • 2012-02-27
  • 1970-01-01
  • 2020-07-24
  • 2012-12-30
  • 2014-07-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多