tree Command: Visualizing Directory Structure

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

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 -L written 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

Lina: Linny-senpai, I wanted to check how a project folder is organized.
Lina: So I keep doing ls, then cd, then ls again. I still cannot picture the whole structure.
Linny-senpai: That is the classic "lost in ls" problem. Diving in one level at a time and backing out makes you lose track of where you are.
Lina: Exactly. I want to draw it out on paper.
Linny-senpai: That drawn-out diagram is what the 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 tree for the current location, or tree PATH to target a specific place
  • For huge folders, tree -L 2 (limit depth) and tree -I 'node_modules' (exclude) are essential

1. What Is tree?

Conclusion: tree displays a folder's contents as a branching diagram, so you see the whole hierarchy without running ls over and over.

Lina: How is tree different from ls?
Linny-senpai: ls lists only the contents of one location. tree walks down through every level below and shows them as a branching diagram.
Lina: So it is like running ls automatically in every folder?
Linny-senpai: That is the right idea. And the parent-child relationships are drawn with connecting lines.
Linny-senpai: So you instantly see which folder a given config file lives under.

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: tree is often not installed by default. Install it with apt or dnf.

Lina: I want to try it right away. How do I install it?
Linny-senpai: On some distributions it is not there out of the box.
Linny-senpai: If 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 tree with no arguments to show the current directory; use tree PATH to target a specific location.

Linny-senpai: Usage is very simple. Let's start by running it with no arguments.

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 ~
Lina: Just typing tree printed everything down to the deepest level.

Lina Gets Stuck: The Screen Would Not Stop Scrolling

Lina: I ran 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
Lina: More than 98,000 files? My project is not that big. Did something break?
Linny-senpai: Nothing broke. By default, tree walks all the way down to the bottom level.
Linny-senpai: So a huge folder like node_modules gets printed in full, every single entry.
Lina: I only wanted to see the files I wrote. Almost all of that count was libraries.
Linny-senpai: Right. So limit the depth with -L and drop the folders you do not need with -I. Those two keep the output readable.
Lina: That makes sense. I will start with -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 NUMBER to limit how many levels are shown. For huge folders, start with -L 1 or -L 2 to grasp the big picture.

Lina: How do I stop it from printing too much, like before?
Linny-senpai: Use -L (Level). -L 2 shows only down to the second level.
Linny-senpai: That lets you see the overall shape in a moment.
# 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 0 is 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: -d shows directories only, -a includes hidden files, and -I excludes unwanted folders.

Linny-senpai: After depth, the next step is choosing what to show. Master this and tree becomes far more practical.

Directories only (-d)

# When you only want the folder structure
$ tree -d -L 2
Lina: That looks handy when there are too many files and I only want the folder skeleton.
Linny-senpai: Exactly. -d (directory) hides every file and shows only the folder skeleton.
Linny-senpai: It helps a lot when you explain 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'
Lina: node_modules has tens of thousands of files. That is what flooded my screen earlier.
Linny-senpai: Right. So -I (Ignore) is the standard fix.
Linny-senpai: Separate patterns with | (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: -F adds type indicators, -h --du shows cumulative directory sizes, and -o FILE saves the output to a file.

Lina: Is there a way to add a bit more information?
Linny-senpai: There is. Showing file types and sizes alongside the tree makes investigation much easier.

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.

Lina: If it shows sizes, can I use it for disk usage like ncdu?
Linny-senpai: As a snapshot of one moment, yes. But you cannot drill down with arrow keys or delete on the spot.
Linny-senpai: For that interactive work, ncdu is the better fit. Think of 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.

Lina: I have the knowledge now. I want to try it by hand.
Linny-senpai: Good, I prepared three exercises. 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

  1. Running without -L on a large folder -> the screen scrolls like a waterfall. Start with -L 1 or -L 2
  2. Panicking when hidden files do not appear -> tree hides dot-prefixed entries by default. Check whether you forgot -a
  3. 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

Lina: Let me sum up. Unlike ls, tree walks the levels below and draws them as a diagram.
Linny-senpai: Right. That is why the default settings flood the screen on a huge folder.
Lina: So I stop the depth with -L and drop unwanted folders with -I.
Linny-senpai: Well remembered. For a folder you have never seen, start at -L 2. That one step keeps the output readable.

Today's 3-Line Summary

  1. tree draws a directory structure as a branching diagram, replacing repeated ls calls
  2. -L NUMBER limits the depth. Start at -L 2 for a folder you are seeing for the first time
  3. -d shows folders only, -a includes hidden files, and -I 'PATTERN' excludes what you do not need

Next Reading

Share this article

Next steps