Learn › Linux Foundations › Navigation
tree - a hands-on Linux lab on a real virtual machine.
See a whole directory hierarchy at a glance with tree. Depth with -L, directories only with -d, hidden files with -a, one branch by path, all on the recon-base filesystem.
You just used ls to look inside a folder. On Linux a folder is called a directory, and the two words mean the same thing. But ls cannot answer a basic question in one look: how much is really nested under recon-base? ls shows you only one floor at a time. To see what is inside ops/, you run ls again. To see inside ops/plan-alpha/, you run it again.
There is a command that answers that question in one shot, drawing the whole building at once, every floor as one diagram. It is called tree.
The black boxes in this lesson are a practice terminal: a safe sandbox that only checks the one command each step teaches, so you cannot break anything. tree is not part of a bare Linux install, so the practice terminals just replay its output here. On the real machine at the end of the module, the mission installs tree for you before you run it.
You are back on the same box from the last lesson. Callsign: recon-base. Before you draw the whole thing, take the smallest possible peek. Type this exact command and press Enter:
tree -L 1 ~/recon-base
prompt: student@linuxcamp:~$ answer: tree -L 1 ~/recon-base output: /home/student/recon-base
`-- ops
6 directories, 2 files hint: Type tree, then a space, then -L, a space, the number 1, and the path. Like this: tree -L 1 ~/recon-base
Same seven names ls showed you, but look at the shape. Those lines on the left (|-- and ` -- `) are tree drawing branches, the way a family tree draws lines between relatives. -- `` marks the last item in a group.
The -L 1 you typed means "go only 1 level deep." You asked for the top floor and nothing below it. Look at the two shortcuts too: broken-link and latest-briefing each print an arrow (->) to whatever they point at, and tree spells out the full path of the target. In the next step you take the walls off.
tree lists a directory, then everything inside it, then everything inside those, drawing the whole nested structure as one indented diagram. Where ls shows a single level, tree shows all of them.
That indented, branching shape is called a hierarchy: things arranged in levels, each one nested inside the one above it. Your files live in a hierarchy, and tree is the command that draws a picture of it.
The -L you used is short for level. tree -L <number> sets how many levels deep to draw. -L 1 is the top floor only. Leave -L off and tree goes all the way down.
tree and ls are partners, not rivals. Reach for ls when you want the plain contents of one folder, and tree when you want the shape of everything below it.
Sometimes you do not care about individual files. You want the skeleton: which directories exist and how they nest. That is what the -d flag gives you. -d stands for directories, and it tells tree to draw folders only and skip every file.
This is the fastest way to understand how a project is organized. Aim it at the whole base:
tree -d ~/recon-base
prompt: student@linuxcamp:~$ answer: tree -d ~/recon-base output: /home/student/recon-base
-- ops |-- plan-alpha |-- plan-bravo -- plan-charlie
9 directories hint: Add the -d flag between tree and the path. Like this: tree -d ~/recon-base
Every file vanished, and now the structure jumps out. ops/ is the only folder with rooms of its own: three plan directories nested one level below it. Notice the summary at the bottom changed too. With -d it counts directories only, and there is no file count at all.
Notice something else: .classified is not here. It is a hidden directory, one whose name starts with a dot, and tree skips hidden entries by default, exactly like ls does. You will pull it out of hiding in two steps.
You do not have to draw the entire base every time. Give tree a path and it draws just that branch. This is how you focus on one part of a big system without the rest flooding your screen.
The ops/ directory holds the operation plans. Draw only that subtree, with all its files this time:
tree ~/recon-base/ops
prompt: student@linuxcamp:~$ answer: tree ~/recon-base/ops output: /home/student/recon-base/ops
`-- run-scan.sh
4 directories, 8 files hint: Type tree, a space, then the path to the ops folder. Like this: tree ~/recon-base/ops
One command, the whole operation. You can read the plans at a glance: three plan folders, each with its own objectives, plus a run-scan.sh script sitting at the top level. The vertical bars (|) carry a branch downward so you can trace which file belongs to which folder.
See how deep the indentation goes? That is tree earning its name. Running ls four times could not show you this shape in one look.
Back in the -d step, .classified was missing. Hidden entries, the ones whose names start with a dot, stay invisible until you ask for them. The -a flag asks for them. -a stands for all, and it tells tree to include hidden files and directories.
The .classified directory is the sealed dossier from the last lesson. Draw it with everything shown:
tree -a ~/recon-base/.classified
prompt: student@linuxcamp:~$ answer: tree -a ~/recon-base/.classified output: /home/student/recon-base/.classified
`-- top-secret.txt
1 directory, 3 files hint: Add the -a flag between tree and the path. Like this: tree -a ~/recon-base/.classified
There it is. .hidden-orders was invisible to a plain tree, and -a dragged it into view. Notice where it lands in the list: dot-names sort ahead of ordinary names, so .hidden-orders sits at the top.
Whenever a folder looks emptier than it should, add -a. Configuration files and setup files almost always hide behind a leading dot.
Scaffolding off. No command is shown this time. You have every piece you need.
You want to see what ops/ holds at the top, the three plan folders and the script, but without letting the plans spill their files across your screen. Aim tree at the ops path, then stop it after a single level.
prompt: student@linuxcamp:~$ answer: tree -L 1 ~/recon-base/ops output: /home/student/recon-base/ops
`-- run-scan.sh
4 directories, 1 file hint: Think back to your first peek, where a flag capped the depth at one level. Point tree at the ops path and do the same.
Nailed it. The depth cap stopped tree at the top of ops: the three plan folders and run-scan.sh are all listed, but tree never descended, so none of the plans' files cluttered the view. Notice the count followed suit, 4 directories, 1 file (the root ops plus the three plan folders it drew). You combined a flag and a path from memory, which is exactly the move the real machine will ask of you.
You earned this cheat sheet. Every row is a form of tree you have already run:
Stack the flags when you want to: tree -d -L 2 ~/recon-base draws directories only, two levels deep. tree is one of the first commands to reach for on any unfamiliar machine, because in one screen it answers the question every other command dances around: what is actually in here?
On a giant folder, plain tree can scroll for pages. Tame it with -L 2 for a quick overview first, then zoom into the branch you care about with a path.
The practice terminal has shown you the shape. The Navigation module ends with one real machine, and that is where you draw these trees for real.
In the Navigation capstone mission, a Linux VM boots just for you on the same recon-base filesystem. The mission installs tree, then hands you an objective that points it at one branch of the base. Every flag you just learned works there too: try -L for depth, -d for directories, or -a for hidden files once the objective is green. One difference from here: the lab shows no commands. You read the objective, you recall the tool, you type it. That recall is what makes it stick.
Finish the other Navigation lessons, then go get the aerial view for real.
Practice The tree Command in a real Linux terminal at The Linux Camp. Progress is verified automatically as you type commands on the machine.