Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filtering output of `tree` command

Tags:

bash

I have a directory containing many sub-directories, each with lots of files having different extensions.

Is it possible for me to filter the output of the tree command so that files having certain extensions like say .log and .msh or certain named directories are not in the output

Consider the example below

    .
    ├── dir1
    │   ├── bar.log
    │   ├── blahblah
    │   │   └── blah.txt
    │   ├── hello.txt
    │   └── test.msh
    ├── foo.log
    ├── out
    ├── test.py
    └── test.txt

    2 directories, 8 files

I would like to filter away certain directories (and obviously their contents) and files having certain extensions. For instance I would like to filter away the contents of directory blahblah and all files having extensions .msh and .log

So I would like the output to be

.
├── dir1
│   ├── hello.txt
├── out
├── test.py
└── test.txt
like image 521
smilingbuddha Avatar asked Mar 08 '26 21:03

smilingbuddha


1 Answers

tree command has -I option that allows you to filter out both files and directories that match the given pattern.

tree -I 'blahblah|*.msh|*.log'
like image 78
afenster Avatar answered Mar 12 '26 07:03

afenster



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!