Nifty command lines: tree
This post is part of the Nifty Command Lines series.
The tree
command is an easy way to print files and folder structures. Like you see in a lot of tutorials. Running it without arguments will print your files and folders.
~/code-like-the-90s β―β―β― tree
.
βββ css
β βββ main.css
βββ img
β βββ bkg.png
βββ index.html
βββ script.js
2 directories, 4 files
Ignoring
You can tell it to ignore directories with tree -I [DirectoryName]
. In most web scenarios, youβll want to ignore your npm packages, so tree -I node_modules
.
You can exclude more than one folder by separating them with a pipe character.
tree -I 'node_modules|.cache|test_*|public'
This is a must to ignore cache folders, build directories, etc.
Restricting output
The -d
flag restricts it to only show your folders and hide files.
~/c/nextjs-app β―β―β― tree -d
.
βββ pages
β βββ api
βββ public
βββ styles
4 directories
The -P
flag allows you to only show a certain type of files. For example, if you want to list your JavaScript files while ignoring npm packages.
~/my-app β―β―β― tree -P '*.js' -I 'node_modules'
.
βββ public
βββ src
βββ App.js
βββ App.test.js
βββ index.js
βββ serviceWorker.js
βββ setupTests.js
2 directories, 5 files
Finally, you can restrict how deep in the folder structure you want to crawl using the -L
flag. tree -L 2
will only go 2 levels deep.
As usual, install it with Homebrew or apt-get: brew install tree
or apt-get install tree
There you go! Iβve used this command quite a lot to create documentation or get a sense of a new codebase. I hope it will be useful for you too.
Cheers