Drop `ls` for `tree` in your day to day unix shell experience

manthony
2 min readApr 27, 2022

--

You cd into a huge directory- maybe you are at root looking for a certain config or file that is swallowed up by a sea of log files, dot-files, and other directories. You run ls and there you sit, scanning through the output with a keen eye trying to decide where to go next- the destination is unknown.

…or maybe instead you are refactoring a huge monolithic application and have spent quite a while refactoring and rearchitecting the organization of the app. You want to present it at standup or run it by someone for a second opinion.

tree is a great tool that has many of the features of ls with a much prettier print and can help you out in both of these predicaments- lets look at an example.

ls

. config.dat data data1.bin data2.sql data3.inf images background.jpg icon.gif logo.jpg program.exe readme.txt

tree

.
├── config.dat
├── data
│ ├── data1.bin
│ ├── data2.sql
│ └── data3.inf
├── images
│ ├── background.jpg
│ ├── icon.gif
│ └── logo.jpg
├── program.exe
└── readme.txt
2 directories, 9 files

And just like that, no more ls for you. There are plenty of flags that are useful to expand or narrow down the tree that is returned, here are some I use often:

-a

Print hidden dot files or “all”, akin to ls -a

-d

List directories only

-f

Prints the full path prefix for each file

-l

Follows symbolic links if they point to directories, as if they were directories. Symbolic links that will result in recursion are avoided when detected.

Be sure to check out the manpage for all of the options and find new ways to make it suit your needs. Always try and apply the tool in the ways that best help you solve your unique problem!

You can even alias this command in your .bashrc or .bashprofile (or zsh/fish if you roll that way) and have just as short of a time

alias t=tree

And maybe even have specific versions of it to list what you need using some flag commands like this one that will search a passed pattern

function tree_seek() {
tree -P "*$1*"
}

alias ts='tree_seek'

Hope that this helps you out- if you are on many flavors of linux you should already have, otherwise mac users I recommend downloading homebrew and installing with brew install tree

--

--