tree Command: Visualizing Directory Structure
What You'll Learn
- How to grasp a directory 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, and anyone tired of ls and cd to navigate folders
Intro: Lina's "Lost in ls" Moment
ls, then cd, then ls again... but I just can't picture the whole structure.tree command gives you in one shot. It shows a folder's contents as a branching diagram. Let's learn it today.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 all the levels 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 (└──is the last item)│: a vertical line showing the branch continues below- Indentation depth = 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. After installing, run tree --version to confirm the version.
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! ...Though it scrolled off the screen a bit.tree walks all the way down to the bottom level. So running it as-is on a large folder makes the screen scroll like a waterfall. Next let's learn how to rein that in.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. That lets you quickly see just the overall shape.# 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're seeing for the first time, drill down from shallow to deep:
tree -L 1then-L 2 - Specify depth as an integer of 1 or more (
-L 0is an error) - Go shallow when you want a "map," deeper for a specific 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. It's great when explaining how a project is organized.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, so tree just scrolls forever, right...-I (Ignore) is the standard fix. Separate patterns with | (pipe) to exclude several. node_modules, .git, and dist are the usual suspects.Conversely, to see "only these," use -P
When you want to show only a certain pattern, use -P (Pattern). tree -P '*.js' keeps only .js files. If empty folders get in the way, add --prune to drop folders that don't contain a 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 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 (the same idea as the du command). Combined with -h (human readable), you get readable units like 1.2K or 3.4M. It's handy for a rough sense of which folder is eating space.
ncdu?tree as "draw the structure" and ncdu as "hunt down and clean up space."Save the result to a file (-o)
# Write the tree to a text file $ tree -L 2 -o structure.txt
When adding a "Directory structure" section to a README, paste the output of tree -L 2 for an instant layout diagram. Strip noise with -I before pasting for a clean result.
7. Mini Exercises: Try It in Your Own Environment
Conclusion: Three tasks (depth limit, directories only, exclude) lock in
tree's practical options.
Task 1: Run tree -L 1 on your home directory to see what sits directly under it.
Task 2: Run tree -d -L 2 ~ to show only the folder skeleton under your home.
Task 3: In any project folder, run tree -I '.git' and confirm that .git is excluded.
Hint for Task 1
tree -L 1 ~
Limiting to a single level with -L 1 lists just the direct contents as a tree, like ls.
Hint for Task 2
tree -d -L 2 ~
-d hides files and -L 2 limits depth to two levels, so only the folder skeleton shows.
Hint for Task 3
tree -I '.git'
Write the name to exclude after -I. .git disappears, leaving just the source structure. To exclude several, separate them with |, like '.git|node_modules'.
8. Common Pitfalls
Conclusion: Watch out for running unlimited depth on huge folders, forgetting hidden files, and forgetting to quote exclude patterns.
Three frequent mistakes
- Running without
-Lon a large folder -> the screen scrolls like a waterfall onnode_modulesand friends. Start with-L 1or-L 2. - Panicking when hidden files don't appear ->
treehides dot-prefixed entries by default. Check whether you forgot-a. - Forgetting to quote the exclude pattern -> without single quotes, as in
-I node_modules|.git, the shell interprets|as a pipe and you get unexpected behavior. The correct form is-I 'node_modules|.git'.
Safe, practical patterns
- First look at a folder:
tree -L 2(shallow overview) - Explaining a layout:
tree -d -L 2(skeleton only) - Browsing a repo:
tree -L 2 -I 'node_modules|.git|dist'(drop the noise)