DEV Community

Serhat Teker
Serhat Teker

Posted on • Originally published at tech.serhatteker.com on

Ignore Directories in Tree

tree displays the structures of the given directory.

For instance below command,

$ tree src/
Enter fullscreen mode Exit fullscreen mode

would show something like below:

src
├── core
│   ├── __init__.py
│   └── __pycache__
│       └── __init__.cpython-36.pyc
└── templates
    └── .gitkeep

3 directories, 3 files
Enter fullscreen mode Exit fullscreen mode

If no directory given it shows the current directory's structure:

.
├── config
│   ├── __pycache__
│   │   ├── settings.cpython-36.pyc
│   │   ├── urls.cpython-36.pyc
│   │   └── wsgi.cpython-36.pyc
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── .editorconfig
├── .env
├── .gitignore
├── LICENSE
├── Makefile
├── manage.py
├── .pytest_cache
│   ├── CACHEDIR.TAG
│   ├── .gitignore
│   ├── README.md
│   └── v
│       └── cache
│           ├── lastfailed
│           ├── nodeids
│           └── stepwise
├── pytest.ini
├── README.md
├── requirements.txt
├── run
│   ├── db.sqlite3
│   └── .gitkeep
├── Session.vim
├── src
│   ├── core
│   │   ├── __init__.py
│   │   └── __pycache__
│   │       └── __init__.cpython-36.pyc
│   └── templates
│       └── .gitkeep
└── tests
    ├── .gitkeep
    └── __pycache__
        └── basic_test.cpython-36-pytest-5.4.1.pyc

12 directories, 29 files
Enter fullscreen mode Exit fullscreen mode

To ignore directory/directories you can give -I flag. For instance to ignore __pycache__ directories:

$ tree -I __pycache__
Enter fullscreen mode Exit fullscreen mode
.
├── config
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── .editorconfig
├── .env
├── .gitignore
├── LICENSE
├── Makefile
├── manage.py
├── .pytest_cache
│   ├── CACHEDIR.TAG
│   ├── .gitignore
│   ├── README.md
│   └── v
│       └── cache
│           ├── lastfailed
│           ├── nodeids
│           └── stepwise
├── pytest.ini
├── README.md
├── requirements.txt
├── run
│   ├── db.sqlite3
│   └── .gitkeep
├── Session.vim
├── src
│   ├── core
│   │   └── __init__.py
│   └── templates
│       └── .gitkeep
└── tests
    └── .gitkeep

9 directories, 24 files
Enter fullscreen mode Exit fullscreen mode

To exclude multiple directories you can use pipe, |:

$ tree -I '__pycache__|*test*'
Enter fullscreen mode Exit fullscreen mode
.
├── config
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── .editorconfig
├── .env
├── .gitignore
├── LICENSE
├── Makefile
├── manage.py
├── README.md
├── requirements.txt
├── run
│   ├── db.sqlite3
│   └── .gitkeep
├── Session.vim
└── src
    ├── core
    │   └── __init__.py
    └── templates
        └── .gitkeep

5 directories, 16 files
Enter fullscreen mode Exit fullscreen mode

This command will skip __pycache__ and all directories that match *test* regex expression.

All done!

Top comments (0)