How to use the command tree (with examples)
The tree
command is a useful tool for displaying the contents of a directory as a tree-like structure. It provides a visual representation of the directory hierarchy, making it easier to navigate and understand.
Use case 1: Print files and directories up to ’num’ levels of depth
Code:
tree -L num
Motivation: This use case is helpful when you want to limit the depth of the tree output and only see files and directories up to a certain level. It can be useful to get a high-level overview of the directory structure without overwhelming details.
Explanation:
-L num
: Specifies the maximum depth to display. Replacenum
with the desired number of levels.
Example output:
.
├── file1.txt
├── file2.txt
└── directory
├── file3.txt
└── nested_directory
└── file4.txt
2 directories, 4 files
Use case 2: Print directories only
Code:
tree -d
Motivation: This use case is beneficial when you want to focus solely on directories within a specific directory. It helps to quickly identify the directory structure without cluttering the output with individual files.
Explanation:
-d
: Specifies that only directories should be displayed.
Example output:
.
├── directory1
└── directory2
└── nested_directory
└── nested_directory2
3 directories
Use case 3: Print hidden files too with colorization on
Code:
tree -a -C
Motivation: This use case is useful when you want to include hidden files in the tree output and have them displayed in a different color. It helps to reveal files that are typically hidden by default.
Explanation:
-a
: Displays all files and directories, including hidden ones.-C
: Enables colorization of the output for better visibility.
Example output:
.
├── file1.txt
├── .hidden_file
└── directory
├── file2.txt
└── .hidden_directory
1 directory, 3 files
Use case 4: Print the tree without indentation lines, showing the full path instead
Code:
tree -i -f
Motivation: This use case is handy when you want a compact view of the directory tree without indentation lines. It shows the full path of each file and directory, making it easier to locate specific items.
Explanation:
-i
: Disables indentation lines in the output.-f
: Displays the full path instead of using indentation for hierarchy.
Example output:
.
./file1.txt
./file2.txt
./directory
./directory/file3.txt
./directory/nested_directory
./directory/nested_directory/file4.txt
2 directories, 4 files
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: This use case is valuable when you want to get insights into the sizes of files and directories within a given directory. It helps to identify space-consuming items and manage disk storage efficiently.
Explanation:
-s
: Shows the size of each file and the cumulative size of each directory.-h
: Displays the sizes in human-readable format (e.g., 1K, 234M, 2G).--du
: Uses the “du” command for size calculation.
Example output:
.
├── 234K file1.txt
├── 456K file2.txt
└── 2.3M directory
├── 1.1M file3.txt
└── 1.2M nested_directory
└── 1.2M file4.txt
2 directories, 4 files
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: This use case is helpful when you want to filter the tree output and only include files that match a specific pattern. It makes it easier to locate and analyze specific types of files within a directory.
Explanation:
-P '*.txt'
: Specifies the pattern using a wildcard (glob) to match specific files (e.g., all files with a “.txt” extension).--prune
: Prunes out directories that don’t contain matching files.
Example output:
.
├── file1.txt
├── file2.txt
└── directory
└── file3.txt
1 directory, 3 files
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 directory_name --matchdirs --prune
Motivation: This use case is useful when you want to focus on specific directories within a tree and exclude the rest. It helps to narrow down the output to only the relevant directories.
Explanation:
-P directory_name
: Specifies the directory name to match.--matchdirs
: Matches only directories.--prune
: Prunes out directories that aren’t ancestors of the wanted one.
Example output:
.
└── directory
└── nested_directory
1 directory
Use case 8: Print the tree ignoring the given directories
Code:
tree -I 'directory_name1|directory_name2'
Motivation: This use case is beneficial when you want to exclude specific directories from the tree output. It helps to filter out unnecessary directories and focus on the relevant parts of the directory hierarchy.
Explanation:
-I 'directory_name1|directory_name2'
: Specifies the directories to ignore, separated by the pipe (|) character.
Example output:
.
├── file1.txt
├── file2.txt
└── directory
└── nested_directory
└── file4.txt
2 directories, 3 files
Conclusion:
The tree
command is a versatile tool for visualizing directory structures. With its various options and arguments, you can customize the output to suit your specific needs. Whether you want to limit the depth, show only directories, include hidden files, or filter specific files or directories, the tree
command has got you covered. Use it to gain insights into your directory hierarchy and navigate it more efficiently.