How to install the tree command in Ubuntu
First you need to install the tree as it is not available by default; So if you haven’t installed the tree command yet, use the following command to install it:
How to use the tree command in Ubuntu
Once it’s installed; Run the following command for the detailed usage of the tree command. The syntax of the tree command is:
tree [options] [directory]
The syntax consists of two elements: Options and directory: Options are used to display the result of the tree command according to a specific property. For example, options can be used to get the output in several ways, such as: in alphabetical order to show only directories, to check the size and location of files or directories. However, if you want to get the result of a specific directory, use the directory name in place of the “directory” keyword used in the syntax.
General command output : You can print the contents of the directory in the tree structure (on the terminal) by running the following command. The command outputs the content of the current working directory:
The tree command displayed a total of 18 directories and 13 files:
How to use options with the tree command in Ubuntu
We have seen common usage of tree commands; In addition, there are various options that are used to perform certain tasks within the current directory or the path of another directory.
How to use the -a option with the tree command
In general, the tree command does not print the contents of hidden directories; this option also helps to print the contents of hidden directories; The syntax for using this flag is shown below:
Tree -a [directory]
The command given below helps to get the hidden contents of the current working directory:
You can observe that the tree command only returned 18 directories and 13 files while; the same command if he is using the “-a”Flag, 215 directories and 256 files are displayed.
How to use the -d option with the tree command
This option is used with the tree command to list only the directories of the target directory; the syntax of the tree command with the option “-D“Is given below:
Tree -d [directory]
The command given below will only print the directories within the current directory:
Unlike the present working directory; For example, you would like the directory listing of the “Desktop”Directory and execute the following command:
How to use the -f option with the tree command
Tree command if it starts with the “-F”Option, displays the path prefix of each file in a tree structure: The syntax for using this option is shown below:
Tree -f [directory]
The command given below will display the path of any directory or file that exists in the current directory:
In addition, you can get the result of this command for any directory other than pwd; For example, in order to-F“Flag on the”Desktop“Directory; use the path of the directory or you can pass the name of the directory if it is a subdirectory of your pwd.
How to use the -o option with the tree command
You can use this option to get the output of the Tree command in any other file; The syntax for using this flag is given below:
tree [directory] -Ö [filename]
The command given below saves the output of the tree command in a file named “Output.txt“:
You can get the output of a specific directory for this option: The following command saves the output of the tree command (desktop directory) in the “Edition1.txt“File:
How to use the -Q option with the tree command
This tree file listing option displays the names of files in double quotes; The syntax for using this flag is given below.
Tree -Q [directory]
To print the filenames in double quotes; Run the following command to get the output of the current working directory:
In addition, the command given below shows the file and directory name (of the desktop directory) in double quotes:
How to use the -p option with the tree command
By using the “-P”Flag, all file types and permissions for each file are printed in a hierarchical structure: The syntax of the tree along with this option is given below:
syntax
Tree -p [directory]
The command given below shows the use of this option in the current working directory:
Use of “-P“Flag for the directory with the name”Directory1“; which is available in our home directory: The following command returns the result of the “-P“Flag on”Directory1“:
How to use the -s option with the tree command
You can view the size (in bytes) of all files by clicking the “-S“Flag; the syntax is given below:
Trees [directory]
The command given below shows the size of the files in the current working directory:
To check the size of a particular directory; pass the path, or you can only pass the name if the directory is in the current working directory: The following command outputs the size of the files it contains “Directory1“:
How to use the -h option with the tree command
This option also shows the size of each file and directory, but in KB, MB, GB, etc. The “-S”Flag shows the result in bytes, but the result only contains numeric values; An inexperienced user may not be able to obtain the unit of measure.
Tree -h [directory]
The command given below helps to get directories, files and their sizes in tree form:
The command mentioned below leads to the result of “-H” to the “Directory1“:
How to use the -t option with the tree command
This option sorts the files and displays the output as the modification time of the files; the syntax of the “-T“Flag with tree command is given below:
Tree -t [directory]
The command given below prints the files in alphabetical order according to their modification time:
In our case, the following command shows the result of the “-T“Flag (on the”Desktop“Directory):
How to use the -r option with the tree command
This option sorts the output of the tree command in reverse alphabetical order; the syntax is shown below:
Tree -r [directory]
The command given below prints the files in reverse alphabetical order of the current directory:
To get the alphabetical list of files included in “Directory1“, Run the following command:
How to use the -L option with the tree command
This option limits the number of branches of the tree command; The syntax for using this option is given below:
Tree -L [numeric number] [directory]
This option limits the number of branches of a tree to 1:
To shortlist the “Directory1“Directory; the following commands will do the job:
diploma
Ubuntu supports several useful commands for dealing with directories and files; Tree command is one of them. In this guide, the tree command is explained in detail; its basic use is to print the directories and files in a tree-like hierarchical structure; In addition, dozens of flags are supported by a tree command that displays output with specific properties. You can combine multiple options at the same time for cleaner results with a single command.