如何使用树来显示包含特定字符串的文件名的路径?

2 command-line tree

我有一个相当大的文件系统,假设我想在文件系统的某个区域中找到包含 String 的所有文件和目录,ActionListeener为此我想使用tree. 所以如果我这样做:

tree ~/ | grep ActionListeener
Run Code Online (Sandbox Code Playgroud)

结果将是:

?   ?   ?           ??? ActionListeener.class
?   ?           ??? ActionListeener.java
Run Code Online (Sandbox Code Playgroud)

所以我现在知道这些文件确实存在于我的文件系统更深的某个地方,但我不知道哪个文件夹包含子文件夹,哪个文件夹包含子文件夹,等等。

所以我的问题是,我怎样才能获得树命令(通过将输出管道传输到其他东西)来显示文件夹和路径,这些文件夹和路径将我引导到包含该字符串的那些特定文件?


操作系统信息:

Description:    Ubuntu 15.04
Release:    15.04
Run Code Online (Sandbox Code Playgroud)

包装信息:

tree:
  Installed: 1.7.0-3
  Candidate: 1.7.0-3
  Version table:
 *** 1.7.0-3 0
        500 http://gb.archive.ubuntu.com/ubuntu/ vivid/universe amd64 Packages
        100 /var/lib/dpkg/status
Run Code Online (Sandbox Code Playgroud)

hee*_*ayl 7

使用-f树的选项。从man tree

-f 打印每个文件的完整路径前缀。

所以你的命令将是:

tree -f ~/ | grep 'ActionListeener'
Run Code Online (Sandbox Code Playgroud)

请注意,这将匹配ActionListeener行中的任何位置,因此请精确选择要在哪个目录中运行它。


mur*_*uru 7

tree的模式匹配 ( -P) 与 结合使用--prune

$ tree
.
??? archlinux-simplyblack
?   ??? angle-down.png
?   ??? archlinux.png
# snip
    ??? reboot.png
    ??? shutdown.png
    ??? theme.conf

8 directories, 78 files
$ tree -P 'reboot*' --prune            
.
??? maui
?   ??? reboot.png
??? maui-dark
    ??? reboot.png

2 directories, 2 files
Run Code Online (Sandbox Code Playgroud)

来自man tree

-P pattern
      List  only  those files that match the wild-card pattern.  Note:
      you must  use  the  -a  option  to  also  consider  those  files
      beginning   with  a  dot  `.'   for  matching.   Valid  wildcard
      operators are `*' (any zero or more characters), `?' (any single
      character),   `[...]'   (any  single  character  listed  between
      brackets (optional - (dash) for character range may be used: ex:
      [A-Z]),  and  `[^...]'  (any  single  character  not  listed  in
      brackets) and `|' separates alternate patterns.

--prune
      Makes  tree prune empty directories from the output, useful when
      used in conjunction with -P or -I.  See BUGS AND NOTES below for
      more information on this option.
Run Code Online (Sandbox Code Playgroud)