LinuxCommandLibrary

tree

Display directory structure as a tree

TLDR

Print files and directories up to 'num' levels of depth (where 1 means the current directory)

$ tree -L [num]
copy

Print directories only
$ tree -d
copy

Print hidden files too with colorization on
$ tree -a -C
copy

Print the tree without indentation lines, showing the full path instead (use -N to not escape non-printable characters)
$ tree -i -f
copy

Print the size of each file and the cumulative size of each directory, in human-readable format
$ tree -s -h --du
copy

Print files within the tree hierarchy, using a wildcard (glob) pattern, and pruning out directories that don't contain matching files
$ tree -P '[*.txt]' --prune
copy

Print directories within the tree hierarchy, using the wildcard (glob) pattern, and pruning out directories that aren't ancestors of the wanted one
$ tree -P [directory_name] --matchdirs --prune
copy

Print the tree ignoring the given directories
$ tree -I '[directory_name1|directory_name2]'
copy

SYNOPSIS

tree [OPTIONS] [DIRECTORY...]

PARAMETERS

-a
    All files are listed, including hidden files (those starting with a dot '.').

-d
    List directories only, effectively showing only the directory structure.

-f
    Prints the full path prefix for each file and directory.

-i
    Makes tree not print the indentation lines, useful for large outputs or specific parsers.

-L level
    Descend only 'level' directories deep. 'level' is an integer.

-P pattern
    List only those files that match the wildcard 'pattern'. Directories are still printed.

-I pattern
    Do not list files that match the wildcard 'pattern'.

-p
    Prints the file type and permissions for each file as in `ls -l`.

-u
    Displays the username (or UID if no username found) of the file owner.

-g
    Displays the group name (or GID if no group name found) of the file owner.

-s
    Prints the size of each file in bytes.

-h
    Prints the size of each file in a human-readable format (e.g., K, M, G).

-D
    Prints the date of last modification for each file.

-t
    Sorts the output by last modification time, newest first.

-r
    Reverse the order of the sort.

-F
    Appends a '/' for directories, '*' for executables, '@' for symbolic links, etc., to file names.

-C
    Turn colorization on. This is usually the default when outputting to a TTY.

--noreport
    Omits the final report of the number of directories and files generated.

DESCRIPTION

The tree command is a powerful and intuitive utility in Linux and Unix-like systems, designed to display the contents of directories in a hierarchical, tree-like structure. It provides a visual representation of the file system, showing directories, subdirectories, and files in an indented format that clearly illustrates their relationships. Unlike the ls command which typically lists contents of a single directory, tree recursively traverses the specified path, presenting a comprehensive overview of the entire directory tree. This makes it invaluable for understanding complex file structures, documenting project layouts, or quickly locating files without navigating through each directory individually. It supports various options for controlling the depth of traversal, displaying file permissions, sizes, modification dates, and even filtering files based on patterns, making it a versatile tool for system administrators, developers, and general users alike.

CAVEATS

1. Performance on large directories: Running tree on very large directory structures (e.g., / or /usr) without a depth limit (-L) can consume significant time and resources, potentially filling the terminal buffer.
2. Network mounts: Performance can degrade significantly when traversing directories on slow network file systems (NFS, SMB).
3. Overwhelming output: For deep or wide directory trees, the output can be extremely long and difficult to parse visually without filters or depth limits.
4. Default behavior: By default, tree does not show hidden files (those starting with a dot '.' on Unix-like systems) unless the -a option is specified.

OUTPUT FORMATS

Beyond standard text output, tree supports generating output in various structured formats, making it useful for scripting and integration with other tools:
-H: HTML output, creating a valid HTML file with clickable links.
-J: JSON output, representing the directory structure as a JSON array.
-X: XML output, generating an XML document.

SYMBOLIC LINKS

By default, tree follows symbolic links to directories. To prevent this and only list the link itself, use the -l option (follow symbolic links as files). To cross file system boundaries and not descend into directories mounted on different file systems, use the -x option.

USEFUL COMBINATIONS

The true power of tree lies in combining its options:
tree -L 2 -d: Lists directories only, up to 2 levels deep.
tree -a -s -h: Shows all files (including hidden), their human-readable sizes.
tree -I 'node_modules|*.log': Excludes specific directories or file types.

HISTORY

The tree command was originally developed by Steve Baker in 1996. It was created to provide a more intuitive and visually appealing way to list directory contents compared to traditional commands like ls -R. Its simplicity, effectiveness, and ability to clearly represent file system hierarchy quickly led to its widespread adoption. tree is now a standard utility available in most Linux distributions and Unix-like operating systems, often included by default or easily installable via package managers. Its core functionality has remained consistent, focusing on providing a clear, depth-indented view of files and directories.

SEE ALSO

ls(1), find(1), du(1), pwd(1)

Copied to clipboard