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 [-adfghilnprsSuvxACDFQ] [-L level] [-R] [-H baseHREF] [-T title] [-o filename] [--nolinks] [-P pattern] [-I pattern] [--inodes] [--device] [--noreport] [--dirsfirst] [--version] [--help] [--filelimit #] [--si] [--du] [--timefmt format] [--sort name|size|mtime|ctime|version] [--matchdirs] [--ignore-case] [--charset charset] [--colour] [--color] [-N] [--unicode] [--ascii] [--c] [--permissions] [--quotes] [-b] [--] [directory ...]

PARAMETERS

-a
    All files are listed. By default, tree does not list hidden files (those beginning with a dot '.').

-d
    List directories only.

-f
    Prints the full path prefix for each file.

-g
    Prints the group name of the file.

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

-i
    Makes tree not print the indentation lines, useful when used in conjunction with the -f option.

-l
    Follows symbolic links to directories as if they were directories.

-n
    Turns colorization off globally.

-P pattern
    Only list those files that match the wild-card pattern.

-I pattern
    Do not list files that match the wild-card pattern.

-L level
    Max display depth of the directory tree.

-s
    Prints the size of each file in bytes.

-u
    Prints the username of the file.

-D
    Prints the date of last modification or creation time.

--inodes
    Prints the inode number of the file or directory.

--device
    Prints the device number to which the file or directory belongs.

-v
    Sort output by version.

-x
    Stay on the current file-system only. Like find -xdev.

-A
    Draw lines using ASCII characters.

-C
    Turn colorization on always.

DESCRIPTION

The tree command is a recursive directory listing program that produces a depth-indented listing of files. With no arguments, tree lists the files and directories in the current directory. When directory arguments are given, tree lists all the files and directories found in the given directories each in turn. Upon completion of listing all files/directories found, tree returns the total number of files and/or directories listed.

tree is a powerful and user-friendly tool for visualizing directory structures, particularly useful for navigating complex file systems, documenting project layouts, or quickly identifying file organization.
It automatically adjusts the output to fit within the terminal's width. Options allow controlling the depth of the listing, excluding certain files or directories, and altering the output format.

CAVEATS

Large directory structures can result in very long outputs. Using the -L option to limit the depth is recommended in such cases. Symbolic link loops can cause tree to recurse indefinitely. Use with caution when following symbolic links (-l option).

EXIT STATUS

Tree returns 0 on successful completion. Non-zero values are returned if tree encounters an error, such as being unable to open a directory.

SEE ALSO

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

Copied to clipboard