The `tree` Command in Ubuntu



This content originally appeared on DEV Community and was authored by Siswoyo Siswoyo

📌 Introduction

The tree command in Ubuntu is a powerful utility that displays the contents of directories in a tree-like structure. Instead of listing files and directories in a flat list (like ls does), it visually represents the hierarchy of directories, making it easier to understand the structure of a file system.

This is especially useful for:

  • Viewing project structures
  • Exploring configuration files
  • Understanding nested directories at a glance

📥 Installation

By default, tree may not be installed on Ubuntu. You can install it using:

sudo apt update
sudo apt install tree -y

To confirm installation:

tree --version

📂 Basic Usage

1. Display current directory structure

   tree

2. Show full path of files and directories

   tree -f

3. Limit depth of directory tree

   tree -L 2

(This shows only two levels deep.)

4. Show hidden files (dotfiles)

   tree -a

5. Print size of files

   tree -s

🎯 Common Options

Option Description
-a Show all files including hidden ones
-d List directories only
-L n Limit depth of directory tree to n levels
-f Print full path for each file
-s Show file size in bytes
-h Show file size in human-readable format
--du Show cumulative directory sizes
-P List only files matching a pattern
-I Exclude files matching a pattern

🔍 Examples

1. Display only directories up to 3 levels

   tree -d -L 3

2. Show file sizes in human-readable format

   tree -h

3. Exclude specific directories (e.g., .git)

   tree -I ".git"

4. Display project structure

   tree -L 2 -I "node_modules"

📑 Example Output

If you run tree -L 2 inside a project folder, you may get:

.
├── README.md
├── package.json
├── public
│   ├── index.html
│   └── favicon.ico
└── src
    ├── App.js
    ├── index.js
    └── components

✅ Conclusion

The tree command in Ubuntu is an essential tool for visualizing directory structures. With its various options, it allows developers, system administrators, and everyday users to quickly understand and navigate complex file systems.

Reference: https://manpages.ubuntu.com/manpages/jammy/man1/tree.1.html


This content originally appeared on DEV Community and was authored by Siswoyo Siswoyo