Funnily enough macOS does not come with the shell command
tree. Tree is the handy little command-line program, which lists the directory structure and files contained within. There are ways to install it with third-party tools, but those tools have failed me one time too many, so I have moved away from them.
The "official" tree command comes with a multitude of options, but I really never use them, so almost the bare minimum is enough.
I use zsh as my shell, so I created this shell alias to my
alias tree="pwd && find . -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'"
Now, when I run
tree in a directory, I will get the familiar output:
> devs/projects/gis > tree /Users/me/devs/projects/gis . |____.DS_Store |____requirements.txt |____images | |____a2 | |____a1 |____shapefiles | |____a1.shp | |____a2.shp |____README.md |____requirements-dev.txt |____app.py |____venv | |____bin | | |____pip3.7 | | |____python3 | | |____easy_install | | |____python | | |____pip3 | | |____activate.fish | | |____easy_install-3.7 | | |____python3.7 | | |____pip | | |____activate | | |____activate.csh | |____include | |____pyvenv.cfg | |____lib | | |____python3.7 | | | |____site-packages --- OUTPUT CUT MANUALLY ---
As you may notice, this has an issue. When run within a development project directory, which containes either a Python virtual environment (
venv in this case) or
node_modules in a JS project, the output gets really verbose. In almost every case, I do not want to see those directories, just my own files and subdirectories instead.
To make this happen, I created also
tree2, like so:
alias tree2="pwd && find . -type d \( -path ./node_modules -o -path ./venv \) -prune -o -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'"
This is pretty much the same thing, but it — a bit naively — will skip the top-level directories
node_modules. When run, it will return the result I want:
> devs/projects/gis > tree2 /Users/me/devs/projects/gis . |____.DS_Store |____requirements.txt |____images | |____a2 | |____a1 |____shapefiles | |____a1.shp | |____a2.shp |____README.md |____requirements-dev.txt |____app.py
As you can see, sometimes you don't need to install a huge pile of tools one on top of each other to accomplish what you need. Plus, this was a fun little excercise on shell scripting!