How to Use the Command 'tree' (with examples)
The tree
command is a versatile tool used in Unix-like operating systems to visualize and navigate the filesystem. It generates a graphical representation of the directory tree of the specified path or the current directory by default. This representation helps users to easily comprehend the structure and contents of directories and files. Through various options, users can customize the view based on specific needs, like limiting the depth, filtering contents, printing sizes, and more. Below, we delve into several use cases illustrating the power and flexibility of the tree
command.
Use Case 1: Print files and directories up to ’num’ levels of depth
Code:
tree -L 2
Motivation:
When navigating a deeply nested file structure, it can be overwhelming to see the entire hierarchy at once. Limiting the displayed levels to a specific depth allows users to focus on a manageable section of the directory tree, making it easier to comprehend the immediate layout without unnecessary details.
Explanation:
-L num
: The-L
option sets the maximum display depth of the directory tree. In this example,num
is set to 2, meaning the tree will show the current directory and its immediate children. Going deeper than 2 levels is avoided for simplicity in viewing.
Example Output:
.
├── dir1
│ ├── file1.txt
│ └── file2.txt
├── dir2
│ └── subdir1
└── file3.txt
Use Case 2: Print directories only
Code:
tree -d
Motivation:
When the primary interest lies in understanding the directory structure rather than the individual files within them, displaying directories only can declutter the output. This is particularly useful for administrators mapping out directory organization for restructuring or auditing purposes.
Explanation:
-d
: The-d
option limits the output to directories only, excluding all files from the visual tree representation, thereby presenting a cleaner view focused solely on the directories.
Example Output:
.
├── dir1
├── dir2
│ └── subdir1
└── dir3
Use Case 3: Print hidden files too with colorization on
Code:
tree -a -C
Motivation:
Hidden files and directories (those prefixed with a dot) often store configuration or system information. By default, these are not included in listings. This command is useful for developers or system administrators who need to access and review these hidden files, perhaps for configuration management or troubleshooting, with the added benefit of colorization for better visualization.
Explanation:
-a
: This option includes all files and directories, including hidden ones (those beginning with a dot).-C
: This option enables colorization in the output, which can help differentiate file types and improve readability.
Example Output:
.
├── .hidden_dir
├── .hidden_file
├── dir
│ ├── file
└── file.txt
Use Case 4: Print the tree without indentation lines, showing the full path instead
Code:
tree -i -f
Motivation:
When a user needs detailed information about file paths, perhaps for scripting or documentation purposes, providing the full path can be crucial. Omitting indentation lines makes the output cleaner and more suitable for copying paths directly into scripts or configuration files.
Explanation:
-i
: Removes the indentation lines that are normally drawn in the tree output.-f
: Prints the full path prefix for each file, which can be useful for understanding the exact location of files in deep directory trees.
Example Output:
./dir/file
./file.txt
Use Case 5: Print the size of each file and the cumulative size of each directory, in human-readable format
Code:
tree -s -h --du
Motivation:
Disk usage monitoring is essential for system administrators. By providing the file sizes and the cumulative sizes of directories, this command helps in assessing space utilization, allowing for effective disk space management and aiding in locating directories that might be unnecessarily consuming resources.
Explanation:
-s
: Shows the size of each file in the tree.-h
: Converts file sizes into a human-readable format, utilizing units such as K, M, G, etc.--du
: Computes and displays the disk usage for each directory, making it possible to see which directories consume the most space.
Example Output:
.
├── [4.0K] dir1
│ └── [1.0K] file.txt
└── [2.0K] file.txt
Use Case 6: Print files within the tree hierarchy, using a wildcard (glob) pattern, and pruning out directories that don’t contain matching files
Code:
tree -P '*.txt' --prune
Motivation:
When searching for specific types of files, such as documents, logs, or configuration files within a complex directory structure, it is helpful to filter and display only those files that match certain criteria. This focused approach saves time and reduces the cognitive load of sifting through unrelated files and directories.
Explanation:
-P '*.txt'
: Uses a glob pattern to match files. Here,*.txt
matches all files with the.txt
extension.--prune
: Removes all directories and subdirectories from the output that do not contain files matching the given pattern, streamlining the results to only relevant sections of the tree.
Example Output:
.
├── dir1
│ └── file.txt
└── file.txt
Use Case 7: Print directories within the tree hierarchy, using the wildcard (glob) pattern, and pruning out directories that aren’t ancestors of the wanted one
Code:
tree -P dir2 --matchdirs --prune
Motivation:
This command is ideal when focusing on a particular directory and its genealogical subtree. By specifying a directory name, users can isolate and understand its precise context within the larger directory hierarchy, which is beneficial during tasks like directory relocations or assessments.
Explanation:
-P dir2
: Matches directories nameddir2
.--matchdirs
: Ensures that the pattern applies to directories as well.--prune
: Prunes out any directories that are not ancestors of the matched directory.
Example Output:
.
└── dir2
└── subdir1
Use Case 8: Print the tree ignoring the given directories
Code:
tree -I 'dir1|dir2'
Motivation:
There are times when parts of the directory tree, like log directories or temporary files, are irrelevant to the user’s current task. Ignoring these directories can help streamline the output, focusing on the more pertinent parts of the filesystem.
Explanation:
-I 'dir1|dir2'
: This option specifies a pattern for directories to ignore. Using a pipe|
allows for the specification of multiple directory names, excludingdir1
anddir2
from the output.
Example Output:
.
└── dir3
└── file3.txt
Conclusion
The tree
command provides a flexible and visual way to examine the filesystem’s structure, adaptable to a wide range of needs through its robust option set. Whether you’re limiting your view to certain depths, focusing solely on directories, or filtering specific files, these use cases demonstrate how tree
can be tailored to simplify file system navigation and management tasks. The examples clearly illustrate the practicality and power of tree
in various scenarios, making it a valuable tool for users looking to enhance their interaction with the command line.