1

Is there anyway to get tree to behave such that, when it is run with the -P option, it will not output directories that don't have files matching the pattern somewhere inside of them?

As an example, take this directory structure:

${DIRECTORY}/
 runtime/
  runtime_library.so
  runtime_library.a
 my_libraries/
  drawing/
   drawing.so
  basic_functions/
   basic.a
 documents/
  manual.txt
 empty/

I'd like to get tree to output this:

${DIRECTORY}/
 my_libraries/
  basic_functions/
   basic.a
 runtime/
  runtime_library.a
Melab
  • 4,048
  • 2
    My version of tree v1.7.0 (on fedora 23) has an option --prune to not show directories that have no matching files. – meuh May 20 '17 at 08:53
  • That's what I need, thanks. It's confusing that without --prune, all files are listed in blue and only matching ones in red, not very helpful. – WesternGun Mar 15 '24 at 09:57

1 Answers1

2

I guess you only want directories matching a specific pattern, so you need the -P option, i.e.

tree -P "*.a" --prune

The above will return all directories containing files with the extension .a. The man page of tree can help you further. For instance, to ignore a pattern, you can use option -I.

The --prune option will suppress output of sections of the tree where directories don't contain files matching the pattern.

AdminBee
  • 22,803
nikolas
  • 463