Tree in linux command

Linux ‘tree Command’ Usage Examples for Beginners

The tree is a tiny, cross-platform command-line program used to recursively list or display the content of a directory in a tree-like format. It outputs the directory paths and files in each sub-directory and a summary of a total number of sub-directories and files.

The tree program is available in Unix and Unix-like systems such as Linux, as well as DOS, Windows, and many other operating systems. It features various options for output manipulation, from file options, sorting options, to graphics options, and support for output in XML, JSON and HTML formats.

In this short article, we will show how to use the tree command with examples to recursively list the contents of a directory on a Linux system.

Learn tree Command Usage Examples

The tree command is available on all if not most Linux distributions, however, if you do not have it installed by default, use your default package manager to install it as shown.

# yum install tree #RHEL/CentOS 7 # dnf install tree #Fedora 22+ and /RHEL/CentOS 8 $ sudo apt install tree #Ubuntu/Debian # sudo zypper in tree #openSUSE

Once installed, you can proceed further to learn the tree command usage with examples as shown below.

1. To list directory content in a tree-like format, navigate to the directory you want and run tree command without any options or arguments as follows. Remember to invoke sudo to run the tree in a directory that requires root user access permissions.

It will display the contents of the working directory recursively showing sub-directories and files, and a summary of the total number of sub-directories and files. You can enable the printing of hidden files using the -a flag.

List Directory Content in Tree Format

2. To list the directory contents with the full path prefix for each sub-directory and file, use the -f as shown.

List Directory Content with Location

3. You can also instruct tree to only print the subdirectories minus the files in them using the -d option. If used together with the -f option, the tree will print the full directory path as shown.

$ sudo tree -d OR $ sudo tree -df

List Sub-Directory Paths

4. You can specify the maximum display depth of the directory tree using the -L option. For example, if you want a depth of 2, run the following command.

Читайте также:  All reader для линукс

Set Directory Listing Depth

Here is another example about setting maximum display depth of the directory tree to 3.

Set Maximum Directory Listing Depth

5. To display only those files that match the wild-card pattern, use the -P flag and specify your pattern. In this example, the command will only list files that match cata* , so files such as Catalina.sh, catalina.bat, etc. will be listed.

List Files by Wild Card Pattern

6. You can also tell the tree to prune empty directories from the output by adding the —prune option, as shown.

7. There are also some useful file options supported by tree such as -p which prints the file type and permissions for each file in a similar way as the ls -l command.

Print File Type and Permissions

8. Besides, to print the username (or UID if no username is available), of each file, use the -u option, and the -g option prints the group name (or GID if no group name is available). You can combine the -p , -u and -g options to do a long listing similar to ls -l command.

Print File Type and Permissions with Owner

9. You can also print the size of each file in bytes along with the name using the -s option. To print the size of each file but in a more human-readable format, use the -h flag and specify a size letter for kilobytes (K), megabytes (M), gigabytes (G), terabytes (T), etc..

$ sudo tree -f -s OR $ sudo tree -f -h

Print File Size

10. To display the date of the last modification time for each sub-directory or file, use the -D options as follows.

Show Date of Last Modification Time

11. Another useful option is —du , which reports the size of each sub-directory as the accumulation of sizes of all its files and subdirectories (and their files, and so on).

12. Last but not least, you can send or redirect the tree’s output to filename for later analysis using the -o option.

$ sudo tree -o direc_tree.txt

That’s all with the tree command, run man tree to know more usage and options. If you have any questions or thoughts to share, use the feedback form below to reach us.

Источник

tree Command Examples in Linux

The tree command is excellent for viewing the structure of a directory and its subdirectories in Linux. Learn how to use it with practical examples.

Читайте также:  Установить заббикс агент на linux

You are already familiar with the directory structure in Linux. It is like the roots of a tree.

And that’s exactly the concept of the tree command. It shows the contents of the current directory and its subdirectories in a tree like fashion.

Tree command in Linux

Before showing the examples of the tree command, let me quickly discuss how to install it.

Install tree

The tree utility does not come pre-installed in most Linux distributions. But you can find it in the official repositories and easily install it.

Once done, all you need to do is append the directory or directory path to the tree command and it will show file contents in a tree manner:

But the tree command can do more than just listing files in Linux. Here are some handful examples.

List only directories

If you want to list only the directories at the specified location, you can use the -d option.

list directories only using the tree command

List hidden files

By default, the tree command won’t list hidden files but this behavior can be altered by using the -a option.

And here, I have compared how it differs when used alongside the default manner (without any options):

find hidden files in linux using tree command

Include the path of files

If you want to get the path for each file, all you need to do is use the -f option and it gets you the path of every file:

list path of every file in directory using the tree command

But what about getting the full path? Well, you just have to append the full path of a directory (from home to the target one) as shown:

tree -f /home/sagar/Directory

And it is lengthy. So let me share a neat way.

In this case, you can utilize the value pwd that holds the value full path. So change the directory where you want to use the tree command.

And use the following command and it will get you the full path of every file present in that directory:

include full path of every file in tree command

List files and directories based on the level

So if the directory has hundreds of subdirectories and if you only want to list the first certain levels such as want to include the first one or two subdirectories, you can use this feature.

You can specify the levels by appending the level (in numbers) to the -L option:

For example, here, I listed files that are at level 2:

list files based of file levels in tree command

And you can see the difference where on the left side, it showed every file present whereas, on the right side (where I used level 2), it only brought the files & directories placed at level 2.

Читайте также:  Patch with diff in linux

List files with permission

To attach file permissions with every file, all you need to do is append the -p option:

But to make it convenient, I would suggest pairing it with -h option for better readability.

For example, Here I went with listing every file present in the MUSIC directory:

List files with permission using the tree command

Get the file size of a directory using the tree command

The tree command can show you the size of each file and directory at a specified location and will also sum the size for you in the end.

For that, you will have to use —df option and I would recommend pairing it with the -h for better readability:

tree --df -h TargetDirectory

Get the file size of a directory using the tree command

List files based on the last modification using the tree command

There are two ways of sorting files based on modification:

Sort files based on first modification

To sort files based on modification, you will have to use the -c option and it will sort files that were modified the first by default.

And here, I will also be using the -D option to get the date and time of modification:

List the first modified files using the tree command

Sort files based on the last modification

In this case, you will have to alter the default effect of the -c option which will list files based on the first modification.

Which can easily be altered when paired with the -r option which will reverse the effect of -c option:

sort files based on last modification using the tree command in linux

And you have them sorted for you!

Wrapping up

You can see how easy it is to visualize a nested directory structure with the tree command.

Ever since I installed it first, it’s been an essential part of my Linux command arsenal. I hope you also start using it extensively.

Источник

Tree in linux command

Learn Latest Tutorials

Splunk tutorial

SPSS tutorial

Swagger tutorial

T-SQL tutorial

Tumblr tutorial

React tutorial

Regex tutorial

Reinforcement learning tutorial

R Programming tutorial

RxJS tutorial

React Native tutorial

Python Design Patterns

Python Pillow tutorial

Python Turtle tutorial

Keras tutorial

Preparation

Aptitude

Logical Reasoning

Verbal Ability

Company Interview Questions

Artificial Intelligence

AWS Tutorial

Selenium tutorial

Cloud Computing

Hadoop tutorial

ReactJS Tutorial

Data Science Tutorial

Angular 7 Tutorial

Blockchain Tutorial

Git Tutorial

Machine Learning Tutorial

DevOps Tutorial

B.Tech / MCA

DBMS tutorial

Data Structures tutorial

DAA tutorial

Operating System

Computer Network tutorial

Compiler Design tutorial

Computer Organization and Architecture

Discrete Mathematics Tutorial

Ethical Hacking

Computer Graphics Tutorial

Software Engineering

html tutorial

Cyber Security tutorial

Automata Tutorial

C Language tutorial

C++ tutorial

Java tutorial

.Net Framework tutorial

Python tutorial

List of Programs

Control Systems tutorial

Data Mining Tutorial

Data Warehouse Tutorial

Javatpoint Services

JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.

  • Website Designing
  • Website Development
  • Java Development
  • PHP Development
  • WordPress
  • Graphic Designing
  • Logo
  • Digital Marketing
  • On Page and Off Page SEO
  • PPC
  • Content Development
  • Corporate Training
  • Classroom and Online Training
  • Data Entry

Training For College Campus

JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week

Like/Subscribe us for latest updates or newsletter RSS Feed Subscribe to Get Email Alerts Facebook Page Twitter Page YouTube Blog Page

Источник

Оцените статью
Adblock
detector