0
0
Linux-cliHow-ToBeginner · 3 min read

How to Use Tree Command in Linux: Syntax and Examples

Use the tree command in Linux to display a directory and its contents in a tree-like format. Run tree [options] [directory] to see nested folders and files visually. It helps quickly understand folder structures from the terminal.
📐

Syntax

The basic syntax of the tree command is:

  • tree [options] [directory]

Here:

  • options modify the output (like showing hidden files or limiting depth)
  • directory is the path you want to display (defaults to current directory if omitted)
bash
tree [options] [directory]
💻

Example

This example shows how to use tree to display the current directory structure with all files and folders:

bash
tree
Output
. ├── file1.txt ├── folder1 │ ├── file2.txt │ └── subfolder1 │ └── file3.txt └── folder2 └── file4.txt 3 directories, 4 files
⚠️

Common Pitfalls

Common mistakes when using tree include:

  • Not installing tree first (it may not be pre-installed on all Linux systems).
  • Running tree in very large directories without limiting depth, which can flood the terminal.
  • Expecting color output without --color option or proper terminal support.

To avoid flooding, use tree -L 2 to limit the display to 2 levels deep.

bash
tree -L 1

# Limits the tree depth to 1 level
Output
. ├── file1.txt ├── folder1 └── folder2 2 directories, 3 files
📊

Quick Reference

OptionDescription
-aShow all files including hidden ones
-L Limit the display to levels of depth
-dList directories only
-fPrint the full path prefix for each file
--colorEnable colored output if supported
-hPrint the size of each file in a human-readable format

Key Takeaways

The tree command visually displays directory structures in a nested format.
Use options like -L to limit depth and -a to show hidden files.
Install tree if not available using your package manager (e.g., sudo apt install tree).
Avoid running tree on very large directories without limits to prevent clutter.
Use tree --color for easier reading if your terminal supports colors.