tree Command: Visualizing Directory Structure
What you'll be able to do
- Show a directory structure as a branching diagram with `tree`
- Limit the depth with `-L` so even a huge folder stays readable
- Combine `-d`, `-a`, and `-I` to narrow the output to what you need
Prerequisites (read these first)
What You'll Learn
- How to grasp a directoryA container that organizes files. Same idea as a "folder" on Windows or macOS. structure at a glance with
tree - How to limit depth with
-Lso even huge folders stay readable - How to narrow output with
-d,-a, and-I - How to move from repeated
lscalls to a single structural view
Who this is for: Linux beginners. Anyone tired of using ls and cd to navigate folders.
Words used in this article
- Directory: the same thing Windows and macOS call a folder. "Folder" and "directory" mean the same thing.
- Level: how deeply folders are nested inside one another. Also called depth.
- Hidden file: a file whose name starts with
.. It does not appear in a normal listing. Also called a dotfile. - Option: a flag such as
-Lwritten after the command. It changes how the command behaves. - Current directory: the directory you are in right now. Read it as "where you are".
- ShellAn interactive program that reads the commands you type and runs them.: the program in the terminal that receives what you type. "Shell", "terminal", and "console" refer to almost the same thing.
- Quoting: wrapping text in
'. Inside quotes, symbols are passed through as plain characters.
tree only displays information. It never deletes or rewrites a file, so running it changes nothing in your folders.
Intro: Lina's "Lost in ls" Moment
ls, then cd, then ls again. I still cannot picture the whole structure.tree command gives you in one shot. It shows a folder's contents as a branching diagram.Quick Summary
tree= a command that visualizes directory structure as a tree (branching diagram)- Run
treefor the current location, ortree PATHto target a specific place - For huge folders,
tree -L 2(limit depth) andtree -I 'node_modules'(exclude) are essential
1. What Is tree?
Conclusion:
treedisplays a folder's contents as a branching diagram, so you see the whole hierarchy without runninglsover and over.
tree different from ls?ls lists only the contents of one location. tree walks down through every level below and shows them as a branching diagram.ls automatically in every folder?Here is what the output looks like.
.
├── README.md
├── src
│ ├── index.js
│ └── utils
│ └── format.js
└── tests
└── index.test.js
3 directories, 4 files
How to read the diagram
├──/└──: a file or folder at that level.└──marks the last item│: a vertical line showing the branch continues below- Indentation depth: it maps directly to hierarchy depth
- Last line: the total number of directories and files
2. Installation
Conclusion:
treeis often not installed by default. Install it withaptordnf.
tree prints "command not found", install it.# Ubuntu / Debian $ sudo apt install tree # RHEL / Fedora $ sudo dnf install tree # macOS (Homebrew) $ brew install tree
The package name is tree on every distribution. Once the install finishes, run tree --version to confirm it.
3. Basic Usage
Conclusion: Run
treewith no arguments to show the current directory; usetree PATHto target a specific location.
Show the current directory
# Show the contents below your current location $ tree
Target a specific path
# Show a given directory $ tree /etc # Show your home directory $ tree ~
tree printed everything down to the deepest level.Lina Gets Stuck: The Screen Would Not Stop Scrolling
tree in my project folder. The screen scrolled like a waterfall and only the last line survived.$ tree
(tens of thousands of lines scroll past) 12043 directories, 98217 files
tree walks all the way down to the bottom level.node_modules gets printed in full, every single entry.-L and drop the folders you do not need with -I. Those two keep the output readable.-L 2 and drill down from there.$ tree -L 2 -I 'node_modules|.git'
If the output scrolled past, write tree | less instead. It shows one screen at a time. Inside less, move with the arrow keys and quit with q.
4. Limit the Depth (-L)
Conclusion: Use
tree -L NUMBERto limit how many levels are shown. For huge folders, start with-L 1or-L 2to grasp the big picture.
-L (Level). -L 2 shows only down to the second level.# One level only (direct contents, close to ls) $ tree -L 1 # Down to two levels $ tree -L 2 /var/log
/var/log ├── apt │ ├── history.log │ └── term.log ├── journal ├── syslog └── dpkg.log 2 directories, 4 files
When to use -L
- For a folder you are seeing for the first time, drill down from shallow to deep:
tree -L 1, then-L 2 - Specify the depth as an integer of 1 or more (
-L 0is an error) - Go shallow when you want a map. Go deeper when you want one folder's details
5. Narrow Down What's Shown
Conclusion:
-dshows directories only,-aincludes hidden files, and-Iexcludes unwanted folders.
tree becomes far more practical.Directories only (-d)
# When you only want the folder structure $ tree -d -L 2
-d (directory) hides every file and shows only the folder skeleton.Include hidden files (-a)
# Show dot-prefixed entries like .git and .env too $ tree -a -L 1
tree hides hidden files by default
Files starting with a dot (.), like .bashrc or .git, are not shown unless you add -a (all).
If a config file seems missing, check whether you forgot -a.
Exclude unwanted folders (-I)
# Exclude node_modules from the output $ tree -I 'node_modules' # Exclude several, separated by | $ tree -I 'node_modules|.git|dist'
node_modules has tens of thousands of files. That is what flooded my screen earlier.-I (Ignore) is the standard fix.| (pipe) to exclude several at once. node_modules, .git, and dist are the usual suspects.Conversely, to see only certain files, use -P
When you want to show only one pattern, use -P (Pattern). Writing tree -P '*.js' keeps only .js files.
If empty folders get in the way, add --prune. It drops folders that contain no matching file.
6. Make It Readable and Show Sizes
Conclusion:
-Fadds type indicators,-h --dushows cumulative directory sizes, and-o FILEsaves the output to a file.
Add type indicators (-F)
# Append / to directories and * to executables $ tree -F -L 1
You get the same indicators as ls -F. A trailing / means a folder and * marks an executable file. You can tell them apart at a glance.
Show sizes (-h / --du)
# Show each file's size in human-readable units $ tree -h # Also show directory sizes (cumulative) $ tree -h --du -L 1
What --du means
--du shows each directory's size as the sum of the files beneath it. That is the same idea as the du command.
Combined with -h (human readable), you get readable units like 1.2K or 3.4M. It is handy for a rough sense of which folder is eating space.
ncdu?tree as the tool that draws the structure and ncdu as the tool that hunts down and clears space.Save the result to a file (-o)
# Write the tree to a text file $ tree -L 2 -o structure.txt
This helps when you add a "Directory structure" section to a README. Paste the output of tree -L 2 and you have an instant layout diagram.
Strip the noise with -I before pasting for a clean result.
Note that -o creates the named file and writes the result into it. If a file with that name already exists, its contents are replaced. Run ls first to check for a file with the same name.
7. Mini Exercises: Try It in Your Own Environment
Conclusion: Three tasks (depth limit, directories only, exclude) lock in
tree's practical options.
tree only displays things, so you can run them as often as you like.Exercise 1: Show what sits directly under your home directory, one level only.
Show Hint 1 (Direction)
Use the option that limits depth. You want the direct contents, so pick the shallowest setting.
The word "Level" is the clue.
Show Hint 2 (Command name)
Use tree. Write -L followed by the number, which here is 1.
Your home directory can be written as ~.
Show Answer
$ tree -L 1 ~
/home/user ├── Documents ├── Downloads ├── Videos └── notes.txt 3 directories, 1 file
Limiting to -L 1 covers the same range as ls, drawn as a tree.
Exercise 2: Show only the folder skeleton under your home, down to two levels. No files.
Show Hint 1 (Direction)
Combine two options. One limits the depth. The other hides files.
For the second one, the word "directory" is the clue.
Show Hint 2 (Command name)
The depth is -L 2. Directories only is -d. Both go after tree, side by side.
Show Answer
$ tree -d -L 2 ~
/home/user ├── Documents │ └── work ├── Downloads └── Videos 4 directories
-d removes the files and -L 2 keeps the depth at two levels. Even the last line counts directories only.
Exercise 3: In a project folder, exclude .git from the output and confirm it is gone.
Show Hint 1 (Direction)
Tell the command the name of the folder you do not want to see. This is a different option from the depth limit.
The word "Ignore" is the clue.
Show Hint 2 (Command name)
Use tree -I. Write the name to exclude after -I, wrapped in quotes.
Without the quotes, the shell reads | as a pipe.
Show Answer
$ tree -a -L 1 -I '.git'
. ├── README.md ├── src └── tests 2 directories, 1 file
Even with -a, .git does not appear. That is proof the exclusion worked.
To exclude several, separate them with |, as in '.git|node_modules'.
8. Common Pitfalls
Conclusion: Watch out for running unlimited depth on huge folders, forgetting hidden files, and forgetting to quoteWrapping a string in quote marks (' or ") so it is treated as one single value, e.g. one with spaces in it. exclude patterns.
Three frequent mistakes
- Running without
-Lon a large folder -> the screen scrolls like a waterfall. Start with-L 1or-L 2 - Panicking when hidden files do not appear ->
treehides dot-prefixed entries by default. Check whether you forgot-a - Forgetting to quote the exclude pattern -> the shell reads
|as a pipe. Write it as-I 'node_modules|.git'
Safe, practical patterns
- First look at a folder:
tree -L 2(a shallow overview) - Explaining a layout:
tree -d -L 2(the skeleton only) - Browsing a repo:
tree -L 2 -I 'node_modules|.git|dist'(drop the noise)
9. Review
ls, tree walks the levels below and draws them as a diagram.-L and drop unwanted folders with -I.-L 2. That one step keeps the output readable.Today's 3-Line Summary
treedraws a directory structure as a branching diagram, replacing repeatedlscalls-L NUMBERlimits the depth. Start at-L 2for a folder you are seeing for the first time-dshows folders only,-aincludes hidden files, and-I 'PATTERN'excludes what you do not need