What is the tree Command?
The tree command in Linux is a powerful, user-friendly tool that visually maps directory structures in a hierarchical, tree-like format. Unlike the basic ls command, which lists files and folders linearly, tree reveals the nested relationships between directories and their contents, making it easier to grasp complex folder layouts at a glance.
In this comprehensive guide, you‘ll learn how to install (if not pre-installed), use, and customize the tree command to streamline file management, document projects, or simply admire your system‘s architecture.
Installing the tree Command in Linux
By default, the tree command is not installed on most Linux distributions. To install it, follow the instructions for your specific operating system:
Installation in RHEL / CentOS / Fedora Linux
For RHEL/CentOS versions up to 8, use the following command:
sudo yum install treeFor RHEL 9 and later versions, use the dnf command instead:
sudo dnf install treeInstallation in Debian / Mint / Ubuntu Linux
sudo apt-get install treeInstallation in Apple macOS
If you‘re using macOS, you can install the tree command using the Homebrew package manager:
brew install treeBasic Syntax of the tree Command
The basic syntax of the tree command is:
tree [options]If you run the tree command without any options, it will output the directory structure, starting from the current directory.
Options Available in the tree Command
The tree command offers a wide range of options to customize its output. Here are some of the most commonly used options:
| Option | Description |
|---|---|
--help | Displays the help information for the tree command. |
--version | Outputs the version of the tree command. |
-a or --all | Includes hidden files and directories in the tree. |
-d or --dirs-only | Lists directories only. |
-f or --full-path | Prints the full path prefix for each file. |
-i or --ignore-case | Ignores case when sorting filenames. |
-x | Stays on the current file system only, as with find -xdev. |
-I <pattern> | Do not list those files that match the wild-card pattern. |
-p or --prune | Omits the specified directory from the tree. |
--filelimit # | Do not descend directories that contain more than # entries. |
-t | Sorts the output by last modification time instead of alphabetically. |
--noreport | Omits printing of the file and directory report at the end of the tree listing. |
-s | Prints the size of each file along with the name. |
-u | Prints the username, or UID # if no username is available, of the file. |
-g | Prints the group name, or GID # if no group name is available, of the file. |
-D | Prints the date of the last modification time for the file listed. |
--inodes | Prints the inode number of the file or directory. |
--device | Prints the device number to which the file or directory belongs. |
-F | Appends a / for directories, a = for socket files, a * for executable files, and a | for FIFOs, as per ls -F. |
-q | Prints non-printable characters in file names as question marks instead of the default caret notation. |
-N | Prints non-printable characters as is instead of the default caret notation. |
-r | Sorts the output in reverse alphabetic order. |
--dirsFirst | Lists directories before files. |
-n | Turns colorization off always, overridden by the -C option. |
-C | Turns colorization on always, using built-in color defaults if the LS_COLORS environment variable is not set. |
-A | Turns on ANSI line graphics hack when printing the indentation lines. |
-S | Turns on ASCII line graphics (useful when using Linux console mode fonts). |
-L <level> | Sets the maximum display depth of the directory tree. |
-R | Recursively crosses down the tree each level of directories (see -L option), and at each of them, executes tree again, adding -o 00Tree.html as a new option. |
-H <baseHREF> | Turns on HTML output, including HTTP references. Useful for FTP sites. |
-T <title> | Sets the title and H1 header string in HTML output mode. |
--charset <charset> | Sets the character set to use when outputting HTML and for line drawing. |
--nolinks | Turns off hyperlinks in HTML output. |
-o <file name> | Sends the output to the specified file name. |
Examples of Using the tree Command
Here are some practical examples of using the tree command:
Display the tree hierarchy of a directory
tree -a ./GFGThis command will display the directory structure of the GFG directory, including any hidden files and directories.
List files with a specific pattern
tree -P sample* .This command will list all files in the current directory (and its subdirectories) that match the pattern sample*.
List only files recursively
If you want to list only files (not directories) recursively, you can use the following command:
tree -daifv --noreport . | xargs -I {} tree -aifv -L 1 --noreport {} | xargs -I {} find {} -prune -type fThis command first lists all directories and files, then filters the output to show only the files.
List directories with more than a certain number of files/directories
tree --filelimit 3 ./GFGThis command will only list directories in the GFG directory that have more than 3 files or subdirectories.
List files with their permissions
tree -p ./GFGThis command will display the directory structure of the GFG directory, including the permissions for each file and directory.
Print the device number for each file or directory
tree --device ./GFGThis command will print the device number to which each file or directory in the GFG directory belongs.
Sort the output by last modification time
tree -t ./GFGThis command will sort the output of the GFG directory by the last modification time of the files and directories, instead of the default alphabetical sorting.
Filter the output to display only specific file types
If you want to display only PDF files inside a directory structure, you can use the following command:
tree -aif --noreport | grep "\.pdf$"This command first lists all files and directories, then filters the output to show only the files with the .pdf extension.
Why Use the tree Command Instead of Other Commands?
Many users compare the tree command with other directory listing tools like ls, find, and du. Here‘s why the tree command stands out:
Better Visualization: Unlike
lsandfind, thetreecommand visually represents the directory structure, making it easier to understand the relationships between folders and files.Customizable Output: The
treecommand supports a wide range of options for sorting, filtering, and formatting the output, making it more flexible than the basicls -Rcommand.File and Directory Counts: Unlike the
findcommand, thetreecommand provides a summary of the number of files, directories, and their total sizes at the end of the output.Readable Hierarchy: Unlike the
ducommand, which shows file and directory sizes, thetreecommand makes the folder relationships and hierarchy much clearer and more readable.
Conclusion
The tree command in Linux is a crucial tool used for displaying directory structure in a visually intuitive way. Whether you need to visualize your file system, list directories only, or include hidden files, the tree command provides a wide range of options to customize its output.
By mastering the tree command, you can streamline your file management tasks, better understand complex folder structures, and even document your projects more effectively. So, start exploring the power of the tree command and unlock the secrets of directory visualization in Linux.