tree – How to Find Files Using Tree Command

tree

I really like tree, however would like to combine it with something like find… in particular, I would like to be able to pass to it (or a similar script) as a parameter some file to find
e.g. *.c so that it only prints files that end in .c and the path that leads to them.

So if
e.g. we have tree returning:

.
├── bar
│   ├── b.c
│   └── c.py
└── foo
    └── a.py

then tree -find "*".c should return

.
└── bar
    └── b.c

Is there a parameter that would allow this with tree? If not, how could this be achieved?

Best Answer

For the case you describe, that can be achieved with the tree command's own pattern matching options. Ex. given

$ tree dir
dir
├── bar
│   ├── b.c
│   └── c.py
└── foo
    └── a.py

2 directories, 3 files

then

$ tree -P '*.c' --prune dir
dir
└── bar
    └── b.c

1 directory, 1 file

Note the quoting of pattern '*.c' to prevent the shell from possibly expanding it to a list of matching files in the current directory.

Related Question