Exploring tree command options
The tree
command offers a variety of options that allow you to customize the output and behavior of the command. Here are some of the most commonly used options:
Limiting the depth of the tree
By default, the tree
command will display the entire directory structure, including all subdirectories. However, you can limit the depth of the tree using the -L
option, followed by the maximum depth you want to display. For example, to display only the top-level directories and files, you can use the following command:
$ tree -L 1 /path/to/directory
/path/to/directory
├── file1.txt
├── file2.txt
├── subdirectory1
└── subdirectory2
Excluding certain files or directories
You can exclude specific files or directories from the output using the -I
option, followed by a pattern or regular expression. For example, to exclude all files with the .txt
extension, you can use the following command:
$ tree -I '*.txt' /path/to/directory
/path/to/directory
├── subdirectory1
│ └── file3.jpg
└── subdirectory2
└── file4.jpg
Showing file sizes
To display the size of each file in the tree, you can use the -h
option, which will display the file sizes in a human-readable format (e.g., 1.2M
instead of 1234567
).
$ tree -h /path/to/directory
/path/to/directory
├── file1.txt 12.3K
├── file2.txt 45.6K
├── subdirectory1
│ └── file3.jpg 1.2M
└── subdirectory2
└── file4.jpg 3.4M
The tree
command also allows you to customize the output format using various options, such as:
-C
: Colorize the output
-F
: Append a '/' for directories, a '=' for sockets, a '*' for executables, and a '|' for FIFOs
-P
: Only list files that match the given pattern
These are just a few of the many options available for the tree
command. By exploring these options, you can tailor the output to your specific needs and preferences.