How to View the Linux File System Hierarchy in the Terminal Using the tree Command

Navigating complex, deeply nested directory structures using only the standard ls command can be incredibly tedious. If you want to understand the layout of a massive code repository or see exactly where configuration files are buried, running ls in every single subdirectory takes too much time.

The tree command solves this problem by rendering an intuitive, visual representation of your file system directly within your terminal window.

How to Install the tree Command

Unlike ls or cd, the tree utility is not always installed by default on minimal Linux distributions.

  • On Ubuntu/Debian-based systems: Run sudo apt install tree
  • On CentOS/RHEL-based systems: Run sudo dnf install tree
  • On Arch Linux: Run sudo pacman -S tree

Basic Usage and Formatting

Navigate to the directory you want to inspect (using cd) and simply type:

tree

The terminal will instantly draw a visual branch structure using ASCII characters, displaying every single folder, sub-folder, and file contained within your current location. At the very bottom, it will print a helpful summary calculating the total number of directories and files it found.

Advanced Filtering Options

If you run tree on the root (/) directory, your terminal will be flooded with millions of files. To make the output useful, you must apply flags to filter the results.

  • Limit the Depth: Use the -L flag followed by a number to specify exactly how many levels deep the command should search. To view only the immediate subdirectories and one level below them, type:
    tree -L 2
  • Show Only Directories: If you only want to map out the folder structure and don’t care about the individual files inside them, use the -d flag:
    tree -d
  • Include Hidden Files: By default, tree ignores hidden files and directories (those starting with a dot, like .config). To force the command to show absolutely everything, use the -a flag:
    tree -a

You can combine these flags for granular control. For example, tree -d -L 3 will print a clean map of your folder structure up to three levels deep, completely ignoring individual files.

Get the best tech tips delivered straight to your inbox.

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