I'm trying to figure out if there is a way to get the UNIX command
tree to display only directories that match a specific pattern.
% tree -d tstdir -P '*qm*' -L 1 tstdir |-- d1 |-- d2 |-- qm1 |-- qm2 `-- qm3 5 directories
The man page shows this bit about the switch.
-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|' sepa‐ rates alternate patterns.
I'm assuming that the bit about ...List only those files... is the issue. Am I correct in my interpretation that this switch will only pattern match on files and NOT directories?
@f-hauri looks to have the best reason as to why this doesn't work the way one would think from the switches available in the
tree man page. I missed this bit in the BUGS section.
BUGS Tree does not prune "empty" directories when the -P and -I options are used. Tree prints directories as it comes to them, so cannot accumu‐ late information on files and directories beneath the directory it is printing.
Given this limitation it looks like
tree isn't the best way to accomplish an inclusive filtered list, but a exclusive filtered list would be an alternative way using the
In lieu of this it would look like either shell wildcards, the find command, or a Perl script would be a more appropriate way to accomplish this. See @f-hauri's fine answer for some of these alternative methods.