[Linux] Visualizing Directory Structures as a Tree with the tree Command

目次

Overview

The tree command is a tool that displays the hierarchical structure of files and subdirectories in a visual “tree” format.

Unlike the recursive listing of ls -R, which can be hard to follow, tree allows you to understand the entire overview of a folder and the relationships between files at a glance. It is also very useful for creating documentation.

Note: This tool is not installed by default in many environments, so you may need to install it first.

# For Ubuntu/Debian systems
sudo apt install tree

# For RHEL/CentOS systems
sudo dnf install tree

Specifications (Arguments and Options)

Syntax

tree [options] [directory_name]

Main Options

OptionDescription
-dDisplays only directories, hiding files.
-P [pattern]Displays only files that match the specified wildcard pattern.
-I [pattern]Excludes files that match the specified wildcard pattern.
-pDisplays file permissions (protections).
-uDisplays the name of the file owner (username).
-gDisplays the name of the file group.
-sDisplays file sizes in bytes.
-hDisplays file sizes in human-readable units (K, M, G).
-FAppends a symbol (such as /, *, @) to indicate the file type.
-L [number]Limits the depth of the directory levels to display.
-NPrints non-printable characters as is (prevents garbled Japanese characters).

Basic Usage

Displaying a Directory Structure

Run the command followed by the directory path. If you do not specify a path, it displays the current directory.

tree my_project

Example Output:

my_project
├── config
│   ├── settings.json
│   └── database.yml
├── data
│   ├── raw
│   │   └── input.csv
│   └── processed
├── scripts
│   └── main.py
└── readme.md

5 directories, 4 files

Practical Commands

1. Searching and Displaying Specific Files with Detailed Information

This command displays permissions, owners, groups, and sizes while filtering for a specific extension (e.g., .conf).

# Show permissions(-p), owner(-u), group(-g), and human-readable size(-h)
# Filter for files ending in .conf (-P)
tree -uphg -P "*.conf" /etc/nginx

Example Output:

/etc/nginx
├── [-rw-r--r-- root     root        1.0K]  fastcgi.conf
├── [-rw-r--r-- root     root        2.4K]  mime.types
├── [-rw-r--r-- root     root        1.5K]  nginx.conf
└── [drwxr-xr-x root     root        4.0K]  conf.d
    └── [-rw-r--r-- root     root         500]  default.conf

1 directory, 4 files

2. Checking the Top Part of a Huge Directory

If a directory has too many files, the output will scroll past your screen. Use the head command to see only the beginning.

# Display only the first 5 lines of the directory structure
tree /var/lib/music/artist_data/ | head -n 5

3. Displaying Only the Directory Structure (Excluding Files)

Use the -d option when you only want to understand the folder layout without being distracted by individual files.

tree -d /var/log

Customization Points

  • Exclusion Settings (-I "pattern"): Use this to hide directories like node_modules, .git, or __pycache__.
    • Example: tree -I "node_modules|.git"
  • Level Limits (-L 2): Best for getting a high-level overview without descending into deep subdirectories.
  • File Type Symbols (-F): Adds visual indicators (/ for directories, * for executables), making it easier to distinguish between different types of files.

Important Notes

  • Garbled Characters: If filenames in non-English characters appear as codes like \343\201..., always add the -N option to output the original character codes.
  • Performance Impact: Running tree on a root directory or a directory with millions of files can consume significant CPU and memory. Always use -L or Ctrl+C to control the output.
  • Pattern Quoting: Patterns used with -P or -I should be wrapped in double quotes to ensure the shell doesn’t expand them before the tree command processes them.

Advanced Application

Saving the Directory Structure to a Text File

To save the directory layout for documentation or system reports, use the following redirection. Using --charset=ascii ensures compatibility with any text editor.

Bash

# Output the structure to a file using ASCII characters for the tree lines
tree --charset=ascii -o directory_structure.txt my_project

Conclusion

The tree command is an indispensable tool for visualizing complex directory hierarchies instantly.

Design Tip: Use the ASCII charset when sharing outputs in environments where special characters might not render correctly.

Ideal Use Cases: Understanding project layouts, creating file lists for delivery, and exploring unknown server environments.

Key Configuration: Combine -d for a clean folder-only view and -L to manage the output depth.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次