How to Use the Tree Command to Navigate File Directory in Linux

The tree command is most secure for witnessing your totality brochure texture at a look. It reflects folders and documents in a translucent, tree-pick layout apt in the incurable. You can manipulate how deep it goes, have latent documents, and even conserve the output to a document. In this brief blog post, we’ll dive deep apt into how to render the the majority of of the tree command.
Table of Fabrics
- Installation and Inalienable Syntax
- Coordinating Directory web site Depth
- Having Masked Records and Folders
- Displaying Records by Complimenting Fads
- Underlining File Sizes in Human-Clear Layout
- Setting tree Doning Other Commands
- Keeping or Exporting tree Upshot
Note: implement not perplex the tree command through pstree. The tree command resolves document brochure, while pstree resolves listing running processes.
Installation and Inalienable Syntax
Tree isn’t mounted by default on every Linux distro, but it’s tiny and fast to add. You just have to intake your ration’s unit supervisor. For instance, on Debian/Ubuntu, intake this:
sudo apt install tree
For those of you on Fedora, CentOS, or RHEL contraptions, you’ll yearn to intake:
sudo dnf install treeArch Linux users can intake this:
sudo pacman -S treeAs promptly as mounted, the most natural intake is:
tree
That prints the tree of the current brochure, affirming documents and brochures through attaching pitches upwards and a last run-wearing, pick the digit of brochures and documents. By default, it lists whatever recursively, brandishing documents and brochures in a ordered perceive.
Further, you can intake memorable flags that readjust how the tree displays its output, such as controling deepness, affirming document dimensions, and so on. You can correspondingly specify the training course of the folder you yearn to map out.
tree [options] [directory]By default, the tree color-codes its output to render it even easier to read. Blue (or unbelievable) stands for brochures, environment-friendly identifies executable documents (pick manuscripts or regimen), and white/default stands for secure documents, and so on.
You can correspondingly investigate what’s within any particular brochure. For instance, if you’re in your residence brochure and yearn to browse through your Records folder, purely type:
tree ~/Documents
The output will most distinctly waterfall down your supervise, affirming you the extensive power texture.
Coordinating Directory web site Depth
Sometimes you wear’t yearn the full infinite descent apt into subfolders. Maybe you single treatment about the top two levels, or you single demand a unblemished outlook instead of too much deepness. To render intake a particular degree, you can intake the -L (degree) flag of the tree command. For instance, to stress single two levels, you can intake this:
tree -L 2
You can correspondingly connect it through other flags for much more power. For instance, you can connect -L through the -d company to stress single brochures and forget documents, but upwards to the 3rd degree or deepness:
tree -d -L 3Without limits, it can go steeply, which is fine for miniscule arrangements but unobtainable for beefy brochures.
Having Masked Records and Folders
In the Linux planet, any document or brochure that leaves through a dot (.) is pondered latent. These aren’t typically for day-to-day intake. They’re traditionally arrangement documents or metadata folders, pick “.bashrc”, “.profile”, or the routine “.git” brochure in Git repositories. The tree command by default hides or forgets these latent documents and folders.
Singularly, you can have them through the -a flag:
tree -aPresently, you render intake every brochure, entailing unanimously the pertinent latent arrangement documents. If you yearn latent documents plus deepness manipulate, intake this:
tree -a -L 2
Displaying Records by Complimenting Fads
Sometimes you wear’t yearn to browse through whatever; you’re scouting for something particular. Maybe you single treatment about Python documents, or you’re glancing for unanimously your Markdown documents. The -P company enables you specify a pattern to match.
For instance, if you yearn to browse through single Python documents, dashed:
tree -P "*.py"If you are glancing for unanimously your text documents, intake:
tree -P "*.txt"You can correspondingly intake wildcards and render intake artistic through your trends. Make it viable for’s case you have to position unanimously documents that prelude through config; you can implement that through:
tree -P "config*"As promptly as you intake -P, the tree will most distinctly still stress the brochure texture, but it’ll single sport documents that match your pattern. Vacant brochures, ones that wear’t contain finalizing documents, will most distinctly still appear in the output. If you position this littering, you can intake the --prune company:
tree -P "*.py" --pruneThis brings rid of any vacant brochures from the output, perishable on you a cleaner perceive that reflects single the paths leading to your target documents. One much more thing: if you yearn to flee out tags that match a pattern, intake -I instead of -P.
Underlining File Sizes in Human-Clear Layout
To be devout, for me, witnessing document dimensions in bytes isn’t particularly useful. As promptly as a document is 524288 bytes, I have to skip and implement psychological maths to figure out that’s about 512 KB. The commendable news is, the -h company resolves this beautifully. It displays document dimensions in a human-legible style, the means we literally assume about document dimensions. For instance:
tree -h
Presently, instead of witnessing 524288, you’ll browse through 512K. A document that’s 1048576 bytes becomes 1.0M. This is definite as promptly as you’re anxious to position out where unanimously your disk void went. You can promptly scan wearing the tree and municipality suspiciously beefy documents.
Proper here’s an additional unit I intake traditionally:
tree -hL 2This reflects me two levels deep, through human-legible document dimensions.
Setting tree Doning Other Commands
In Linux, the real power isn’t in any single command, but in how you can connect them. The tree command just upshots text. And also we can hose pipe it to other commands and implement complex operations on it. The hose pipe (|) confiscates the output from the command on the fled and sends out it as input to the command on the apt.
Make it viable for’s case you yearn to dashed a tree on a beefy brochure, but you wear’t yearn it to fly by your supervise for 30 seconds. You consider to be able to scroll wearing it, lookout it, and read it at your very own pace. You can implement this by consolidating tree through the less command:
tree /usr/lib | lessRun this, and your supervise will most distinctly lock onto the initially web page of the tree output.

You can presently intake the arrowhead fulcrums (Up/Down), spacebar to go down a full web page, / coordinated through by a word to lookout the totality tree for that word, and q to quit and antiphon to your provoke.
Further, the -P flag is commendable for puny trends, but what if you seek much more? What if you yearn to position any document or brochure that has the word admin in it, vessel-insensitive? This is a openings for grep, try consolidating it through tree pick this:
tree -a | grep -i "admin"You can correspondingly position every nonsense containing config in the tree by pairing the wc command through the tree pick this:
tree -a | wc -lThis confers an approximate count of pitches upwards.
Keeping or Exporting tree Upshot
Sometimes you have to conserve that tree texture for after that. Maybe you’re documenting a openings, inventing a report, or just yearn to grasp earlier a snapshot of your brochure texture before gaining readjusts. The most natural used means to implement it is filching advantage of redirection pick this:
tree > directory_structure.txtThis conserves the totality tree output to a text document. Paltry, effective, and you can responsive it in any text editor.
You can correspondingly send out an HTML web page through clickable web attaches and collapsible segments by filching advantage of the built-in -H company:
tree -H . > structure.htmlAmenable it in a web browser, and you’ve got an interactive perceive of your brochure texture.
For documents objectives, you can correspondingly conserve the output through particular formatting:
tree -L 3 -a --dirsfirst > project_docs.txtThe --dirsfirst company lists brochures before documents, gaining the output even much more scheduled. This is most secure for README documents or openings documents.
Last but not least, you can correspondingly append to current documents if you’re make-up a bigger document:
tree -L 2 >> documentation.txtThis adds the tree output to the end of your current documents document without overwriting what’s already there.
Last Pointers
This is the natural intake of the tree command. It bargains bunches of other selections you can explore. To learn much more, type info tree in the incurable or investigate the handbook digital. Press q to departure as promptly as you’re done. You can correspondingly explore other definite commands pick ls, find, and du to supervise and perceive your documents in unlike means.
