What is the tree command in Ubuntu?

The command line interface of Linux-based distributions acts as a catalyst for the actions that users take. Like other distributions, Ubuntu also supports a wide variety of commands, and among these, directory management commands are used to handle the operations related to directories, such as:CD”Command is used to change the current working directory; “mkdir”Command is used to create a new directory. Similarly, the tree command prints directories, subdirectories, and their contents in a tree-like structure. It’s almost like a “ls“Command, but the main difference is the print pattern of both commands: the”ls“Command lists the contents, while the”tree”Command shows the output in a tree-like hierarchical structure. In this demonstration, we will give you an in-depth look at the use of tree commands with examples. So let’s start by installing the tree:

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:

$ sudo suitable To install tree

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:

$ tree

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:

$ tree -a

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:

$ tree -D

Unlike the present working directory; For example, you would like the directory listing of the “Desktop”Directory and execute the following command:

$ tree -D /homeland/adnan/Desktop

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:

$ tree -F

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.

$ tree -F Desktop

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“:

$ tree 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:

$ tree Desktop Edition1.txt

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:

$ tree -Q

In addition, the command given below shows the file and directory name (of the desktop directory) in double quotes:

$ tree -Q Desktop

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:

$ tree -P

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“:

$ tree -P 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:

$ tree -S

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“:

$ tree -S 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:

$ tree -H

The command mentioned below leads to the result of “-H” to the “Directory1“:

$ tree -H 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:

$ tree -T

In our case, the following command shows the result of the “-T“Flag (on the”Desktop“Directory):

$ tree -T Desktop

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:

$ tree -R

To get the alphabetical list of files included in “Directory1“, Run the following command:

$ tree -R Directory1

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:

$ tree -L 1

To shortlist the “Directory1“Directory; the following commands will do the job:

$ tree -L 1 Directory1

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.

Related Posts