The tree command in Linux: visualize directory structure

Introduction

In the day-to-day of administrators and developers, quickly knowing the hierarchy of folders and files is essential for navigating, debugging, or documenting projects. The tree command offers a visual tree view that shows the directory structure clearly and compactly, avoiding the need to run multiple ls or find commands. In this article you will learn what tree is, how to install it, its basic syntax, and practical examples you can apply immediately in your terminal.

What is tree?

tree is a small command-line utility that recursively walks a directory and generates a graphical representation of its content using characters such as |, , and . Each depth level is indicated with indentation, allowing you to quickly identify subfolders, files, and their permissions. Although it is not pre-installed on all distributions, its installation is simple and its resource consumption is minimal.

Installation

On Debian/Ubuntu-based distributions, simply run:

sudo apt update && sudo apt install tree

On Fedora or CentOS/RHEL use:

sudo dnf install tree   # Fedora
sudo yum install tree   # CentOS/RHEL

On Arch Linux and derivatives:

sudo pacman -S tree

After installation, the command is available globally and can be invoked simply by typing tree in the terminal.

Basic Syntax

The simplest form is:

tree [opciones] [directorio]

If no directory is specified, tree works on the current directory (.). Some frequently used options are:

  • -a: shows all files, including hidden ones (those starting with .).
  • -d: lists only directories, ignoring

    This post is also available in ESPAÑOL.

Esta obra está bajo una Licencia Creative Commons Atribución 4.0 Internacional para Francesc Roig francesc@vivaldi.net .