如何使用樹顯示包含特定字串的檔案名稱的路徑?

如何使用樹顯示包含特定字串的檔案名稱的路徑?

我有一個相當大的檔案系統,假設我想找到檔案系統某個區域中包含 String 的所有檔案和目錄ActionListeener,為此我想使用tree.所以如果我這樣做:

tree ~/ | grep ActionListeener

結果將是:

│   │   │           └── ActionListeener.class
│   │           └── ActionListeener.java

所以我現在知道這些文件確實存在於我的文件系統中更深處的某個地方,但我不知道哪個資料夾包含子資料夾,其中包含子子資料夾等等。

所以我的問題是,如何獲得 tree 命令(通過將輸出管道傳輸到其他東西)來向我顯示引導我找到包含該字串的特定文件的資料夾和路徑?


作業系統資訊:

Description:    Ubuntu 15.04
Release:    15.04

套餐資訊:

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

答案1

結合使用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

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.

答案2

使用-f樹的選項。從man tree

-f 列印每個檔案的完整路徑前綴。

所以你的命令將是:

tree -f ~/ | grep 'ActionListeener'

請注意,這將匹配ActionListeener行中的任何位置,因此請精確選擇要在哪個目錄中執行它。

答案3

嘗試tree -P '<your_regex_or_file_name>' <tree_root_directory> --prune

在你的情況下: tree -P 'ActionListeener' ~ --prune

相關內容