7

I have got a rather large file system, and let's say that I want to find all the files and directories in a certain area of my file system which contain the String ActionListeener, and to do this I want to use tree. So if I do:

tree ~/ | grep ActionListeener

The result will be:

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

So I now know that these files do exist somewhere a little deeper into my file system, but I have no idea about which folder contains the sub-folder, which contains the sub-sub-folder, and so on, to the files.

So really my question is, how can I get the tree command (by maybe piping the output to something else) to show me the folder and path which leads me to those particular files which contain that String?


OS Information:

Description:    Ubuntu 15.04
Release:    15.04

Package Information:

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

3 Answers 3

15

Use tree's pattern matching (-P) in combination with --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

From 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.
7

Use the -f option of tree. From man tree:

-f Prints the full path prefix for each file.

So your command will be:

tree -f ~/ | grep 'ActionListeener'

Note that this will match ActionListeener anywhere in a line, so be precise in choosing in which directory you will run this.

-1

Try tree -P '<your_regex_or_file_name>' <tree_root_directory> --prune

In your case: tree -P 'ActionListeener' ~ --prune

0

You must log in to answer this question.