How to use an ignore file with tree

2018 December 26

I like using tree to get a visual from the command line of a project’s directory structure, but I don’t want that visual polluted by dependencies, temporary build artifacts, etc. tree takes a -I command-line option to exclude files and directories matching the given glob pattern, or patterns if they are separated with |. For patterns I always want to exclude, I can use an alias for tree:

alias tree='tree -I node_modules|__pycache__'

This will start to grow hairy as I add more patterns. I’d really like to use an ignore file, a la .gitignore. With the help of paste, it’s easy:

alias tree='tree -I "$(paste -d\| -s ~/.treeignore)"'

Now I can list my glob patterns on separate lines in ~/.treeignore and get the behavior I want in a way that’s easier to maintain.