The command tree is not in vain got this name — she is able to portray the structure of all directories on the computer in the tree view. It the thickest branch is the root folder and directories at the first nesting level, and the role of small branches are separate files that are stored somewhere in the depths of memory.
To make the command work on Linux machines, you need administrator privileges to install the corresponding tool — the set out of the box it is not included.
The syntax and options of the tree
The command tree is no different from most of the standard commands and are as follows:
$ tree option
Of options in the command tree many. Here are those that are responsible for displaying the folder tree:
- -a — displays all without an exception files.
- -d — list only directories.
- -l — symbolic links that lead to folders.
- -f — the contents of folders will be shown with a prefix of the path.
- -x — takes into account only the current file system.
- -L — specifies the nesting level for display in the output.
- -R — recursively navigate all levels.
- -P — displays all files whose name matches pattern.
- -I — the exception to the output file whose name matches the pattern.
- -o — print output to a file with the specified name.
- –noreport — forbids the command to display a report on the number of folders and files at the end of the tree.
- –charset — specifies the encoding to display graphics and html files.
- –filelimit — folders that contain more files than specified, will not be shown.
And these options are used to control the display of document titles:
- -q — replace unprintable characters in file names, familiar ?.
- -N — displays non-printable characters in file names as is.
- -Q — encloses file names in double quotes.
- -p for each file specifies its name and the list of allowed actions.
- -u — prints the name or ID of the account under which the file was created.
- -g — prints the name or ID of the user group that has privileges to access the file.
- -s — next to the name of the file displays its size in bytes.
- -h — displays the file size in a more simple, human-readable form, adding to the number abbreviation.
- -D — prints the last modified date of the file (or the last status change in combination with the-c option).
- -F — adds the symbol / for folders = file-socket * for the executable files > for door files | for FIFO special files.
- –inodes prints the inode numbers for files and folders.
- –device — specifies the device number to which belongs the file or folder.
Options for sorting results:
- -v — sorts the results by levels of nesting.
- -t — sorts the results by the last modified date of files and folders.
- -c — sorts the results by date of last status change.
- -U — the results are displayed in the order in which directories are located on disk.
- -r — sorts the results in reverse order.
- –dirsfirst — will be shown first folders, then files.
The display options of the tree:
- -i — removes the lines (“branches” of the tree), the files and folders are displayed in a list.
- -n — does the tree plain.
- -C returns the tree multicolor display after using the-n option.
Next, consider how the tree command in linux can be used in the examples.
Examples of usage tree
The easiest way to use the Linux command tree to print in the terminal just one word:
The result will be a standard presentation of the folder structure. The size of the issue depends on how much junk has accumulated on the hard disk. The author it so that the scroll is not to scroll:
Slightly reduce the amount of information you can, asking the team to show only folders. To do this, use the option -d. And to cut off from the tree more unnecessary branches, set a limit on the number of files that are stored in folders (if there are more files, the folder will not be in the results). This will help us to option –filelimit.
tree -d --filelimit 26
By the way, it is impossible to set a limit less than 25 files.
By default, the tree command in linux is not showing hidden folders. To see them, you should use the option -a. At the same time it will not hurt to sort the results — for example, levels of nesting (parameter -v). Well why not find out when a particular file was last changed — will add to the team also -D.
Now, let’s work with a specific group of files. For example, let’s select the ones that have the pdf to do this allows the option -P. It gives the team understand that you want to display only documents that match the mask. To set a mask for any number of characters from 0 to infinity is a character *, and to indicate only 1 character — sign ?. Name of a file or folder should be enclosed in single quotation marks.
tree -P '*.pdf' --prune
Option –prune is needed in order to exclude from the output folder, within which there are no required documents (by default, the command removes even those folders that are not relevant to the search query).
That’s what we get in the end:
Standard the result of the command tree is sent to the terminal. But you can print it to a file and save it for future use. To do this, create a txt document called tree_command_results and place it in the root directory. Then select the following:
tree -d-o tree_command_results.txt
Option -d is used to reduce the amount of information and its presence here is not required. Option -o is responsible for redirecting output to a file.
In the terminal, no result displayed:
But in the file we find a list of folders, which takes up 45 pages:
For more information about files complete command tree options -h (shows size), -u (indicates the account from which the file was created), -p (so we know what you can do with each file — only view or also change its contents). Also use option -fto see the full path to each document.
Useful life hack — if you combine the options -P and -f, you can quickly find files, lost in the memory of the computer:
tree -f-P '*studio*' --prune
The tree for Linux users is difficult to overestimate. With its help it is possible to obtain all possible information about the files and folders located on your hard drive. By the way, she has a competitor — the more well-known command ls, which, however, is inferior to the tree in convenience and functionality. And how and why you use the tree?