tree Command
The tree command in Linux is a recursive directory listing program that produces a depth indented list of files. It provides a visual representation of the directory structure, making it easy to understand the hierarchy of files and subdirectories.
Syntax
Description
tree is a utility that recursively lists the contents of directories in a tree-like format. It can display files, directories, and optionally, file permissions, user/group ownership, and file sizes. It's particularly useful for visualizing complex directory structures.
Common uses include:
- Visualizing directory structures
- Listing files and directories recursively
- Filtering output to show only directories or specific file types
- Displaying file metadata like permissions and sizes
Common Options
| Option | Description |
|---|---|
-a |
All files are listed. By default, dot files (those that start with a .) are not listed. |
-d |
List directories only. |
-f |
Prints the full path prefix for each file. |
-i |
Makes tree not print the indentation lines, useful when used in conjunction with the -f option. |
-L level |
Descend only level directories deep. |
-p |
Print the file type and permissions for each file as in ls -l. |
-s |
Print the size of each file in bytes along with the name. |
Examples
List current directory contents
Lists the contents of the current directory and its subdirectories in a tree-like format.
List specific directory contents
Lists the contents of the '/var/log' directory.
Display only directories
Displays only directories, excluding files.
Display with full path
Displays the full path to each file and directory.
Limit depth of listing
Lists directories and files up to 2 levels deep from the current directory.