smilingbuddha
smilingbuddha

Reputation: 14660

Filtering output of `tree` command

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

Upvotes: 11

Views: 7755

Answers (1)

afenster
afenster

Reputation: 3608

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'

Upvotes: 19

Related Questions