Sử dụng lệnh “tree” liệt kê thư mục và file theo dạng cây trên Linux

Sử dụng lệnh “tree” liệt kê thư mục và file theo dạng cây trên LinuxCuongquach.com | Chương trình lệnh Tree là một chương trình hỗ trợ cho người dùng liệt kê các file và thư mục tại 1 thư mục theo dạng hình cây rất là đẹp và dễ nhìn, nó sẽ quét sâu hết tất cả các sub thư mục. Sau khi liệt kê hết tất cả thì chương trình ‘tree‘ sẽ trả về thông tin tổng số lượng file hoặc thư mục đã được liệt kê.

Hướng dẫn sử dụng lệnh Tree trên Linux

1. Cài đặt chương trình ‘tree’

Mặc định thì chương trình lệnh ‘tree‘ không được cài đặt trên Linux. Vậy nên nếu chưa có chương trình này bạn cần cài đặt như sau :

+ RHEL/CentOS

# yum install tree

+ Ubuntu/Debian

# sudo apt-get install tree

2. Cấu trúc lệnh ‘tree’

tree /path/to/directory
tree [options]
tree [options] /path/to/directory

2.1 Liệt kê file/thư mục cơ bản

# tree /etc
/etc/
├── adjtime
├── aliases
├── aliases.db
├── alternatives
│   ├── ld -> /usr/bin/ld.bfd
│   ├── libnssckbi.so.x86_64 -> /usr/lib64/pkcs11/p11-kit-trust.so
│   ├── mta -> /usr/sbin/sendmail.postfix
│   ├── mta-aliasesman -> /usr/share/man/man5/aliases.postfix.5.gz
│   ├── mta-mailq -> /usr/bin/mailq.postfix
│   ├── mta-mailqman -> /usr/share/man/man1/mailq.postfix.1.gz
│   ├── mta-newaliases -> /usr/bin/newaliases.postfix
│   ├── mta-newaliasesman -> /usr/share/man/man1/newaliases.postfix.1.gz
│   ├── mta-pam -> /etc/pam.d/smtp.postfix
│   ├── mta-rmail -> /usr/bin/rmail.postfix
│   ├── mta-sendmail -> /usr/lib/sendmail.postfix
│   └── mta-sendmailman -> /usr/share/man/man1/sendmail.postfix.1.gz
├── anacrontab
├── asound.conf
├── audisp
│   ├── audispd.conf
│   └── plugins.d
│   ├── af_unix.conf
│   └── syslog.conf
├── audit
│   ├── auditd.conf
│   ├── audit.rules
│   ├── audit.rules.prev
│   └── rules.d
│   └── audit.rules
├── avahi
│   └── avahi-autoipd.action
├── bash_completion.d
│   ├── git
│   ├── iprutils
│   ├── pip
│   ├── yum
│   └── yummain.py -> yum
....
...
├── yum.conf
└── yum.repos.d
├── CentOS-Base.repo
├── CentOS-CR.repo
├── CentOS-Debuginfo.repo
├── CentOS-fasttrack.repo
├── CentOS-Media.repo
├── CentOS-Sources.repo
├── CentOS-Vault.repo
├── dell-omsa-repository.repo
├── epel.repo
└── epel-testing.repo

198 directories, 987 files

2.2 Liệt kê cả file ẩn

Mặc định lệnh ‘tree’ không liệt kê thông tin các file ẩn trên hệ thống (file bắt đầu bằng dấu chấm ‘.’). Vậy để liệt kê cả những file ẩn thì ta sử dụng option ‘-a‘.

# tree -a /etc/

2.3 Giới hạn số lượng thư mục sub-directory được liệt kê

Bạn muốn giới hạn số lượng sub-directory sẽ được liệt kê là bao nhiêu thì chỉ cần chỉ định option ‘-L‘ với con số muốn giới hạn.

# tree -L 2 /etc/

2.4 Chỉ liệt kê thư mục, không liệt kê file

# tree -d

2.5 Manual

Bạn muốn xem thêm thông tin về các option sử dụng với lệnh ‘tree‘ .

# manual tree

Như vậy là bạn đã biết thêm 1 chương trình lệnh hỗ trợ kiểm tra số lượng file thư mục cực kì dễ nhìn và theo dõi.

Nguồn: https://cuongquach.com/

How to use “tree” command to list directory with tree structure on Linux

Tree command is a application in UNIX/Linux OS that can help you list all files/directories at specific path directory with the format view of tree style. This command can scan recursively sub directory to find all files for listing. It’s very help ful for you, trust me.

Installing and using command tree command on Linux

1. Installing application command ‘tree’

Default, tree is not installed in Linux/UNIX so you can have it with below steps.

+ RHEL/CentOS

# yum install tree

+ Ubuntu/Debian

# sudo apt-get install tree

2. Syntax command

Syntax

tree /path/to/directory
tree [options]
tree [options] /path/to/directory

2.1 Basic default mode command

# tree /etc/
/etc/
├── adjtime
├── aliases
├── aliases.db
├── alternatives
│   ├── ld -> /usr/bin/ld.bfd
│   ├── libnssckbi.so.x86_64 -> /usr/lib64/pkcs11/p11-kit-trust.so
│   ├── mta -> /usr/sbin/sendmail.postfix
│   ├── mta-aliasesman -> /usr/share/man/man5/aliases.postfix.5.gz
│   ├── mta-mailq -> /usr/bin/mailq.postfix
│   ├── mta-mailqman -> /usr/share/man/man1/mailq.postfix.1.gz
│   ├── mta-newaliases -> /usr/bin/newaliases.postfix
│   ├── mta-newaliasesman -> /usr/share/man/man1/newaliases.postfix.1.gz
│   ├── mta-pam -> /etc/pam.d/smtp.postfix
│   ├── mta-rmail -> /usr/bin/rmail.postfix
│   ├── mta-sendmail -> /usr/lib/sendmail.postfix
│   └── mta-sendmailman -> /usr/share/man/man1/sendmail.postfix.1.gz
├── anacrontab
├── asound.conf
├── audisp
│   ├── audispd.conf
│   └── plugins.d
│   ├── af_unix.conf
│   └── syslog.conf
├── audit
│   ├── auditd.conf
│   ├── audit.rules
│   ├── audit.rules.prev
│   └── rules.d
│   └── audit.rules
├── avahi
│   └── avahi-autoipd.action
├── bash_completion.d
│   ├── git
│   ├── iprutils
│   ├── pip
│   ├── yum
│   └── yummain.py -> yum
....
...
├── yum.conf
└── yum.repos.d
├── CentOS-Base.repo
├── CentOS-CR.repo
├── CentOS-Debuginfo.repo
├── CentOS-fasttrack.repo
├── CentOS-Media.repo
├── CentOS-Sources.repo
├── CentOS-Vault.repo
├── dell-omsa-repository.repo
├── epel.repo
└── epel-testing.repo
198 directories, 987 files

2.1 List include all hidden files

In default mode of ‘tree‘, it will not list information of hidden files (files begin with dot ‘.‘ of name file). So you can use option ‘-a’ to see that.

# tree -a /etc/

2.2 Limit the number sub-directory will be listed (scanned)

You want to limit the number sub-directory that ‘tree’ will scan information, you can use option ‘-L <number>‘.

# tree -L 2 /etc/

2.3 Just list directory, no files

# tree -d

2.4 Manual

Find more information about this command ‘tree‘ on Linux, use command ‘manual‘.

# manual tree

 
Now you know additional command like ‘tree‘ to help you list files/directories on server at a specific directory path with beautiful style view is ‘tree‘. Thank you !

Nguồn: https://cuongquach.com/

Quách Chí Cường: Bạn đang theo dõi website "https://cuongquach.com/" nơi lưu trữ những kiến thức tổng hợp và chia sẻ cá nhân về Quản Trị Hệ Thống Dịch Vụ & Mạng, được xây dựng lại dưới nền tảng kinh nghiệm của bản thân mình, Quách Chí Cường. Hy vọng bạn sẽ thích nơi này !
Related Post