How To Install And Use Tree On CentOS?

Published September 23, 2024

Problem: Installing and using Tree on CentOS

The Tree command displays directory structures in a hierarchical format. It's not included by default in CentOS installations. This can make it difficult for users who want to see their file system structure or need Tree for specific tasks.

Installing Tree On CentOS

Checking If Tree Is Already Installed

Before installing Tree, check if it's on your system. Run this command:

which tree

If Tree is installed, this command will show its location. If not, it will give no output.

Using Yum Package Manager

If Tree isn't installed, you can add it using the Yum package manager:

  1. Open a terminal window.
  2. Run this command to install Tree:
sudo yum install tree -y

The -y flag answers "yes" to any prompts during installation.

  1. Wait for the installation to finish. Yum will download and install Tree and any needed dependencies.

  2. After installation, verify it by running:

tree --version

This command will show the version of Tree installed on your system.

If you have issues during installation, update your system by running sudo yum update before trying to install Tree again.

Tip: Using Tree Efficiently

After installing Tree, you can use it to display directory structures. For example, to show the structure of your current directory with a maximum depth of 2 levels, use:

tree -L 2

This command provides a clear, hierarchical view of your files and folders, making it easier to understand your directory layout.

Using The Tree Command

Basic Usage

To display the directory structure using the Tree command, open a terminal and type:

tree

This command shows the directory structure from your current location, including subdirectories and files.

Tip: Colorful Output

Use the -C option to add color to your tree output, making it easier to distinguish between directories and files:

tree -C

Common Options

Tree offers flags to change its output:

  • -L [number]: Limits the depth of the displayed tree. For example, tree -L 2 shows only two levels of directories.

  • -d: Displays only directories, excluding files from the output.

  • -f: Prints the full path for each file and directory.

  • -s: Shows the size of each file in bytes.

  • -h: Displays file sizes in a readable format (KB, MB, GB).

  • -p: Prints file and directory permissions.

  • -u: Shows file ownership information.

Examples of modified outputs:

To display only directories, two levels deep:

tree -L 2 -d

To show full paths with file sizes in a readable format:

tree -f -h

To display directories and files with their permissions and ownership:

tree -p -u

These options let you customize the Tree command output to fit your needs, making it easier to view and analyze your directory structure.

Advanced Tree Usage

Filtering Output

The Tree command has filtering options to change your output:

  • Exclude file types or directories: Use -I to ignore patterns. To exclude .txt files:

    tree -I "*.txt"

    To ignore multiple patterns, separate them with pipes:

    tree -I "*.txt|*.pdf|*.doc"
  • Limit depth of displayed tree: The -L flag limits the directory tree depth. To show two levels:

    tree -L 2

You can combine these options. To show two levels while ignoring .txt files:

tree -L 2 -I "*.txt"

Tip: Use Wildcards for Efficient Filtering

When filtering, use wildcards to match multiple file types or patterns. For example, to exclude all image files, you can use:

tree -I "*.jpg|*.png|*.gif|*.bmp"

This approach saves time and makes your filtering more comprehensive.

Saving Tree Output

Tree lets you save its output:

  • Send tree output to a file: Use > to save the output to a text file:

    tree > directory_structure.txt
  • Format options for saved output:

    • Use -J for JSON output:
      tree -J > directory_structure.json
    • Use -X for XML output:
      tree -X > directory_structure.xml
    • Use -H for HTML output (creates a web page):
      tree -H . > directory_structure.html

These Tree features allow for analysis and documentation of directory structures on your CentOS system.