Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Detailed usage of the Linux tree command

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge of "detailed usage of Linux tree command". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Preface

This article mainly explains the detailed use of the tree command on the Linux system.

The tree command is a small cross-platform command line program that is used to recursively list or display the contents of a directory in a tree format. It outputs directory paths and files in each subdirectory, as well as a summary of the total number of subdirectories and files.

Tree programs can be used in Unix and Unix-like systems such as Linux, as well as in DOS, Windows, and many other operating systems. It provides a variety of options for output operations, from file options, sorting options to graphics options, and supports output in XML, JSON, and HTML formats.

In this tutorial, we will use use cases to demonstrate how to use the tree command to recursively list the contents of directories on a Linux system.

Detailed instructions for using the Linux tree command

two。 Install the tree command on various distributions

Almost all Linux distributions can use the tree command, but if it is not installed by default, you can install it using the system's package manager, as shown below.

(1) install the tree command tool on RHEL/CentOS 7

Yum install tree

(2) install the tree command tool on Fedora 22 + / RHEL/CentOS 8

Dnf install tree

(3) install the tree command tool on the Ubuntu/Debian system

Sudo apt install tree

(4) install the tree command tool on the openSUSE system

Sudo zypper in tree

3. Daily use cases of the tree command

After installation, you can further learn the use of the tree command through the following use cases.

To list the contents of the directory in a tree-like format, change to the desired directory and run the tree command without any options or parameters, as shown below. Some directories require root permission. Use sudo to call root permission to get access.

Tree

Sudo tree

It recursively displays the contents of the working directory, subdirectories and files, as well as a summary of the total number of subdirectories and files. You can use the-a flag to show hidden files.

Sudo tree-a

To list the full path contents of each subdirectory and file using-f, as shown below.

Sudo tree-f

You can also use the-d option to specify that tree prints only the subdirectories and does not display the files inside. If used with the-f option, tree prints the full directory path, as shown below.

Sudo tree-d

Sudo tree-df

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

Sudo tree-f-L 2

The following is a use case that sets the maximum display depth of a directory tree to 3:

Sudo tree-f-L 3

To display only files that match the content of the wildcard, use-P to select and specify your match. In this case, the command will list only files that match cata*, such as Catalina.sh, catalina.bat, and so on.

Sudo tree-f-P cata*

You can also tell tree to remove empty directories from the output by adding the-- prune option, as shown below.

Sudo tree-f-- prune

Tree also supports some useful file options, such as-p, which prints the file type and permissions of each file in a manner similar to the ls-l command.

Sudo tree-f-p

In addition, to print the user name of each file (UID if there is no user name), use the-u option, and the-g option prints the group name (or GID if there is no group name). You can combine the-p,-u, and-g options to execute output similar to the ls-l command, showing details of files and directories.

Sudo tree-f-pug

You can also use the-s option to print the byte size and file name of each file. To print the size of each file in a format that is easier to read, use the-h option and specify large and small letters to represent kilobytes (K), megabytes (M), gigabytes (G), tb (T), and so on.

Sudo tree-f-s

Or

Sudo tree-f-h

To display the date when each subdirectory or file was last modified, use the-D option, as shown below.

Sudo tree-f-pug-h-D

Another useful option is-- du, which displays the disk space occupied by the specified directory.

Sudo tree-f-- du

You can also use the-o option to send or redirect the output of tree to a file name for later analysis.

This is the end of sudo tree-o direc_tree.txt "detailed usage of the Linux tree command". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report