The tree
command is a powerful tool that generates a tree view of the contents of a directory. It is particularly useful when you need to get a quick overview of the files and directories in a particular directory, and the way they are structured.
Overview
The basic syntax for the tree
command is as follows:
tree [options] [directory]
By default, the tree
command will display the contents of the current directory in a tree view format. If you want to see the contents of a specific directory, simply specify the directory path as an argument.
Here’s an example of how to use the tree
command to display the contents of the /usr/share/doc
directory in a tree view format:
$ tree /usr/share/doc
/usr/share/doc
├── acl
│ ├── AUTHORS
│ ├── ChangeLog.gz
│ ├── COPYING
│ ├── NEWS.gz
│ ├── README
│ ├── THANKS
│ └── TODO
├── acpid
│ ├── AUTHORS
│ ├── ChangeLog
│ ├── COPYING
│ ├── NEWS
│ ├── README
│ └── TODO
├── adduser
│ ├── AUTHORS
│ ├── changelog.Debian.gz
│ ├── changelog.gz
│ ├── COPYING
│ ├── README.Debian
│ └── README.gz
...
As you can see, the tree
command displays the contents of the specified directory in a tree view format, with directories displayed as branches and files displayed as leaves.
Options
The tree
command provides a number of options that allow you to customize the output of the command. The following table lists some of the most commonly used options:
Option | Description |
---|---|
-a |
All files are printed. |
-d |
List directories only. |
-f |
Prints the full path prefix for each file. |
-i |
Don’t print indentation lines. |
-l |
Follows symbolic links like directories. |
-n |
Turn colorization off always (-C overrides). |
-o |
Send output to a file instead of stdout. |
-p |
Print the protections for each file. |
-q |
Print non-printable characters as ‘?’. |
-s |
Print the size of each file. |
-u |
Displays file owner or UID number. |
-g |
Displays file group owner or GID number. |
-h |
Print the size of each file in a human-readable format. |
-r |
Sorts the output in reverse alphabetic order. |
-t |
Sorts the output by last modification time. |
-c |
Sorts the output by last status change time. |
-v |
Sorts the output by version. |
-x |
Stay on the current filesystem only. |
-A |
Prints ANSI lines graphic indentation lines. |
-S |
Print with CP437 (console) graphics indentation lines. |
-n |
Turn colorization off always (-C overrides). |
-C |
Turn colorization on always. |
Troubleshooting Tips
If you encounter issues with the tree
command, here are some troubleshooting tips to help you resolve them:
- If you get an error message that says “command not found”, make sure that the
tree
command is installed on your system. You can install it using your package manager (e.g.apt-get install tree
on Debian-based systems). - If you’re not seeing the output you expect, double-check the options you’re using and make sure they’re correct.
- If you’re having trouble understanding the output, refer to the
tree
command’s documentation or use theman
command to get more information about the command.
Notes
- The
tree
command is not included in all Linux distributions by default, so you may need to install it manually. - The
tree
command can be a useful tool for quickly visualizing the structure of a directory and its contents, which can be particularly useful when working with large or complex file systems.