How to Use the tree Command to Visually Map a Directory Structure in Linux

When you are navigating a massive, headless Linux server via the terminal, understanding the architecture of a complex directory is incredibly difficult. The standard ls command only shows you the files sitting directly inside your current folder. If you want to see what is buried five levels deep inside a labyrinth of subdirectories, manually typing cd into every single folder is agonizing. Instead, you can use the powerful tree command to instantly generate a brilliant, color-coded visual map of the entire directory structure.

What Does the Tree Command Do?

The tree command performs a recursive, deep-dive scan of your filesystem. It starts at your current location and violently drills downward, mapping every single folder, sub-folder, and file it finds. It then outputs this data directly into the terminal using beautiful ASCII line-art, creating a visual “family tree” that allows you to instantly comprehend the architecture of thousands of files.

Installing the Tree Tool

Because tree is considered an advanced visual utility, it is rarely installed by default on minimal Linux server distributions.

  • On Ubuntu/Debian: Type sudo apt install tree and press Enter.
  • On CentOS/RHEL: Type sudo yum install tree and press Enter.

How to Map a Directory

If you navigate to the very top of your server (the root / directory) and type tree, the command will attempt to map millions of files, paralyzing your terminal. You must use it strategically.

  1. Navigate to a specific directory you want to analyze (e.g., a complex web server folder like /var/www/html).
  2. Type the raw command: tree
  3. Press Enter.

The terminal will instantly draw a beautiful map showing branches connecting every folder and file. However, if the folder is massive, the output will still scroll wildly off the screen.

Restricting the Drill Depth

To make the map readable, you must restrict how deep the tree command is allowed to dig by using the -L (Level) flag.

  1. Type the following command:

tree -L 2

  1. Press Enter.

This command forces the engine to stop drilling after exactly two levels. It will show you the folders in your current directory, and the files immediately inside those folders, but it will absolutely refuse to map anything deeper. This provides a clean, highly structured overview of your architecture without overwhelming your screen with thousands of deeply buried configuration files.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.