如何根据文件名查找文件的路径?

SSH*_*SSH 24 grep find

我正在尝试settings.xml在我的 Ubuntu 机器中查找文件。我不知道它在哪里,以及它在哪个目录中。

我试过用这个 -

ls -R | grep settings.xml
Run Code Online (Sandbox Code Playgroud)

但它没有向我显示它所在的完整路径.. 有没有我需要尝试的其他命令可以给我完整路径?

use*_*.dz 25

对于快速搜索(但不是确定的):

locate -br '^settings.xml$'
Run Code Online (Sandbox Code Playgroud)

来自man locate

   locate  reads  one or more databases prepared by updatedb(8) and writes
   file names matching at least one of the PATTERNs  to  standard  output,
   one per line.

   -b, --basename
          Match  only  the base name against the specified patterns.  This
          is the opposite of --wholename.
   -r, --regexp REGEXP
          Search for a basic regexp REGEXP.  No PATTERNs  are  allowed  if
          this  option  is used, but this option can be specified multiple
          times.
Run Code Online (Sandbox Code Playgroud)

^$确保只有文件名为settings.xml而不是文件名称中包含 settings.xml将被打印出来。

您可能需要第一次运行:(updatedb作为root)来更新/构建locate.

  • 值得注意的是`updatedb` 在这个命令中的作用——无论如何,`locate` 经常在没有它的情况下失败 (4认同)
  • `locate '*/settings.xml'` 可能在 `locate` 的各种不同实现(如果可用)中更具可移植性。 (4认同)

X T*_*ian 17

通过文件系统缓慢但稳定地搜索,但最终确定。

find / -xdev -name settings.xml
Run Code Online (Sandbox Code Playgroud)

这将需要一些时间,您可能会收到一些权限错误,但它会到达那里。如果您对它可能位于的位置有更多想法,请将第一个目录从 更改//where/you/guess

  • 将 `2>/dev/null` 附加到命令的末尾将抑制所有错误输出(通过将 stderr 重定向到空设备)。 (4认同)
  • `-xdev`:不要下降其他文件系统上的目录。 (2认同)