Các ví dụ dùng lệnh tree Image Các ví dụ dùng lệnh tree

Bài viết này sẽ giới thiệu với các bạn về các ví dụ dùng lệnh tree của hệ điều hành Linux. Trong bài viết này chúng ta sẽ tìm hiểu về lệnh tree cung như các ví dụ về nó.

1. Giới thiệu lệnh tree

Công cụ tree là công cụ hổ trợ trên các hệ điều hành như Unix, Linux, DOS, Windows,... Nó là một chương trình liệt kê thư mục đệ quy tạo ra một danh sách các tệp dưới dạng cây.

Lệnh tree có sẵn trên hầu hết các bản phân phối của hệ điều hành Linux. Nếu chúng chưa được cài đặt mặc định chúng ta có thể thực thi lệnh sau để cài đặt:

  • Đối với bản phân phối Debian/Ubuntu:

    Chạy lệnh sau để cài đặt gói tree:

    apt-get install tree
    
  • Đối với bản phân phối RHEL/CentOS:

    Chạy lệnh bên dưới để thực hiện cài đặt gói tree:

    yum install tree
    
  • Đối với bản phân phối Fedora 22+:

    Chạy lệnh bên dưới để thực hiện cài đặt gói tree:

    dnf install tree
    
  • Đối với bản phân phối openSUSE:

    Chạy lệnh bên dưới để thực hiện cài đặt gói tree:

    zypper in tree
    

Sau khi cài đặt thành công chúng ta có thể sử dụng lệnh tree trên máy tính của chúng ta.

2. Một số ví dụ lệnh tree

👉 Ví dụ 1: Để liệt kê nội dung thư mục theo dạng sơ đồ cây chúng ta thực hiện di chuyển đến thư mục muốn kiểm tra và chạy lệnh tree như bên dưới:

[root@blogd-net-lab01 sysconfig]# tree
.
├── anaconda
├── chronyd
├── console
├── cpupower
├── crond
├── ebtables-config
├── firewalld
├── grub -> ../default/grub
├── ip6tables-config
├── iptables-config
├── irqbalance
├── kdump
├── kernel
├── man-db
├── modules
├── network
├── network-scripts
│   ├── ifcfg-ens192
│   ├── ifcfg-lo
│   ├── ifdown
│   ├── ifdown-bnep
│   ├── ifdown-eth
│   ├── ifdown-ippp
│   ├── ifdown-ipv6
│   ├── ifdown-isdn -> ifdown-ippp
├── nftables.conf
├── rhn
│   ├── allowed-actions
│   │   ├── configfiles
│   │   └── script
│   ├── clientCaps.d
│   └── up2date
├── rsyslog
├── run-parts
├── selinux -> ../selinux/config
└── sshd

Lưu ý: Nêu chúng ta không có quyền truy cập người dùng root chúng ta cần sử dụng lệnh sudo để chạy lệnh tree trong thư mục cần kiểm tra.

👉 Ví dụ 2: Để liệt kê nội dung thư mục cùng với đường dẫn đầy đũ của mỗi thư mục con và file chúng ta kết hợp tùy chọn -f với lệnh tree như bên dưới:

[root@blogd-net-lab01 sysconfig]# tree -f
.
├── ./anaconda
├── ./chronyd
├── ./console
├── ./cpupower
├── ./crond
├── ./ebtables-config
├── ./firewalld
├── ./grub -> ../default/grub
├── ./ip6tables-config
├── ./iptables-config
├── ./irqbalance
├── ./kdump
├── ./kernel
├── ./man-db
├── ./modules
├── ./network
├── ./network-scripts
│   ├── ./network-scripts/ifcfg-ens192
│   ├── ./network-scripts/ifcfg-lo
│   ├── ./network-scripts/ifdown
│   ├── ./network-scripts/ifdown-bnep
│   ├── ./network-scripts/ifdown-eth
│   ├── ./network-scripts/ifdown-ippp
│   ├── ./network-scripts/ifdown-ipv6
│   ├── ./network-scripts/ifdown-isdn -> ifdown-ippp
├── ./nftables.conf
├── ./rhn
│   ├── ./rhn/allowed-actions
│   │   ├── ./rhn/allowed-actions/configfiles
│   │   └── ./rhn/allowed-actions/script
│   ├── ./rhn/clientCaps.d
│   └── ./rhn/up2date
├── ./rsyslog
├── ./run-parts
├── ./selinux -> ../selinux/config
└── ./sshd

👉 Ví dụ 3: Chúng ta có thể sử dụng lệnh tree để in các thư mục con và không in các file bên dưới thư mục hiện hành thì kết tùy chọn -d với tùy chọn -f như bên dưới:

[root@blogd-net-lab01 sysconfig]# tree -df
.
├── ./console
├── ./modules
├── ./network-scripts
└── ./rhn
    ├── ./rhn/allowed-actions
    │   ├── ./rhn/allowed-actions/configfiles
    │   └── ./rhn/allowed-actions/script
    └── ./rhn/clientCaps.d

👉 Ví dụ 4: Khi chúng ta muốn hiển thị tối đa cây thư mục thì sử dụng tùy chọn L cùng với số lượng cây hiển thị như sau:

[root@blogd-net-lab01 sysconfig]# tree -f -L 1
.
├── ./anaconda
├── ./chronyd
├── ./console
├── ./cpupower
├── ./crond
├── ./ebtables-config
├── ./firewalld
├── ./grub -> ../default/grub
├── ./ip6tables-config
├── ./iptables-config
├── ./irqbalance
├── ./kdump
├── ./kernel
├── ./man-db
├── ./modules
├── ./network
├── ./network-scripts
├── ./nftables.conf
├── ./rhn
├── ./rsyslog
├── ./run-parts
├── ./selinux -> ../selinux/config
└── ./sshd

👉 Ví dụ 5: Để có thể hiển thị những tệp tin chúng ta cần tìm kiếm khớp với một tên nào đó bằng lệnh tree kết hợp với tùy chọn P kèm theo là tệp tin cần tìm kiếm như bên dưới:

[root@blogd-net-lab01 sysconfig]# tree -f -P if*
.
├── ./console
├── ./modules
├── ./network-scripts
│   ├── ./network-scripts/ifcfg-ens192
│   ├── ./network-scripts/ifcfg-lo
│   ├── ./network-scripts/ifdown
│   ├── ./network-scripts/ifdown-bnep
│   ├── ./network-scripts/ifdown-eth
│   ├── ./network-scripts/ifdown-ippp
│   ├── ./network-scripts/ifdown-ipv6
│   ├── ./network-scripts/ifdown-post
│   ├── ./network-scripts/ifdown-routes
│   ├── ./network-scripts/ifdown-sit
│   ├── ./network-scripts/ifdown-Team
│   ├── ./network-scripts/ifdown-TeamPort
│   ├── ./network-scripts/ifdown-tunnel
│   ├── ./network-scripts/ifup
│   ├── ./network-scripts/ifup-aliases
│   ├── ./network-scripts/ifup-bnep
│   ├── ./network-scripts/ifup-eth
│   ├── ./network-scripts/ifup-ippp
│   ├── ./network-scripts/ifup-ipv6
│   ├── ./network-scripts/ifup-plip
│   ├── ./network-scripts/ifup-plusb
│   ├── ./network-scripts/ifup-post
│   ├── ./network-scripts/ifup-routes
│   ├── ./network-scripts/ifup-sit
│   ├── ./network-scripts/ifup-Team
│   ├── ./network-scripts/ifup-TeamPort
│   ├── ./network-scripts/ifup-tunnel
│   └── ./network-scripts/ifup-wireless
└── ./rhn
    ├── ./rhn/allowed-actions
    │   ├── ./rhn/allowed-actions/configfiles
    │   └── ./rhn/allowed-actions/script
    └── ./rhn/clientCaps.d

👉 Ví dụ 6: Ngoài ra chúng ta có thể sử dụng lệnh tree với tùy chọn p để hiện thị cây thư mục giống như lệnh ls -l như sau:

[root@blogd-net-lab01 sysconfig]# tree -f -p
.
├── [-rw-r--r--]  ./anaconda
├── [-rw-r--r--]  ./chronyd
├── [drwxr-xr-x]  ./console
├── [-rw-r--r--]  ./cpupower
├── [-rw-r--r--]  ./crond
├── [-rw-------]  ./ebtables-config
├── [-rw-r--r--]  ./firewalld
├── [lrwxrwxrwx]  ./grub -> ../default/grub
├── [-rw-------]  ./ip6tables-config
├── [-rw-------]  ./iptables-config
├── [-rw-r--r--]  ./irqbalance
├── [-rw-r--r--]  ./kdump
├── [-rw-r--r--]  ./kernel
├── [-rw-r--r--]  ./man-db
├── [drwxr-xr-x]  ./modules
├── [-rw-r--r--]  ./network
├── [drwxr-xr-x]  ./network-scripts
│   ├── [-rw-r--r--]  ./network-scripts/ifcfg-ens192
│   ├── [-rw-r--r--]  ./network-scripts/ifcfg-lo
│   ├── [-rwxr-xr-x]  ./network-scripts/ifdown
│   ├── [-rwxr-xr-x]  ./network-scripts/ifdown-bnep
│   ├── [-rwxr-xr-x]  ./network-scripts/ifdown-eth
│   ├── [-rwxr-xr-x]  ./network-scripts/ifdown-ippp
│   ├── [-rwxr-xr-x]  ./network-scripts/ifdown-ipv6
│   ├── [lrwxrwxrwx]  ./network-scripts/ifdown-isdn -> ifdown-ippp
├── [-rw-r--r--]  ./nftables.conf
├── [drwxr-xr-x]  ./rhn
│   ├── [drwxr-xr-x]  ./rhn/allowed-actions
│   │   ├── [drwxr-xr-x]  ./rhn/allowed-actions/configfiles
│   │   └── [drwxr-xr-x]  ./rhn/allowed-actions/script
│   ├── [drwxr-xr-x]  ./rhn/clientCaps.d
│   └── [-rw-r--r--]  ./rhn/up2date
├── [-rw-r--r--]  ./rsyslog
├── [-rw-r--r--]  ./run-parts
├── [lrwxrwxrwx]  ./selinux -> ../selinux/config
└── [-rw-r-----]  ./sshd

👉 Ví dụ 7: Khi chúng ta muốn in tên người dùng hoặc UID nếu người dùng đó không có tên của mỗi file thì chúng ta có thể sử dụng tùy chọn -u in tên người dùng và tùy chọn -g in tên nhóm hoặc GID nếu không có tên nhóm. Ngoài ra chúng ta có thể kết hợp tùy chọn -p, -u-g để liệt kê tương tự như lệnh ls -l.

[root@blogd-net-lab01 sysconfig]# tree -f -pug
.
├── [-rw-r--r-- root     root    ]  ./anaconda
├── [-rw-r--r-- root     root    ]  ./chronyd
├── [drwxr-xr-x root     root    ]  ./console
├── [-rw-r--r-- root     root    ]  ./cpupower
├── [-rw-r--r-- root     root    ]  ./crond
├── [-rw------- root     root    ]  ./ebtables-config
├── [-rw-r--r-- root     root    ]  ./firewalld
├── [lrwxrwxrwx root     root    ]  ./grub -> ../default/grub
├── [-rw------- root     root    ]  ./ip6tables-config
├── [-rw------- root     root    ]  ./iptables-config
├── [-rw-r--r-- root     root    ]  ./irqbalance
├── [-rw-r--r-- root     root    ]  ./kdump
├── [-rw-r--r-- root     root    ]  ./kernel
├── [-rw-r--r-- root     root    ]  ./man-db
├── [drwxr-xr-x root     root    ]  ./modules
├── [-rw-r--r-- root     root    ]  ./network
├── [drwxr-xr-x root     root    ]  ./network-scripts
│   ├── [-rw-r--r-- root     root    ]  ./network-scripts/ifcfg-ens192
│   ├── [-rw-r--r-- root     root    ]  ./network-scripts/ifcfg-lo
│   ├── [-rwxr-xr-x root     root    ]  ./network-scripts/ifdown
│   ├── [-rwxr-xr-x root     root    ]  ./network-scripts/ifdown-bnep
│   ├── [-rwxr-xr-x root     root    ]  ./network-scripts/ifdown-eth
│   ├── [-rwxr-xr-x root     root    ]  ./network-scripts/ifdown-ippp
│   ├── [-rwxr-xr-x root     root    ]  ./network-scripts/ifdown-ipv6
│   ├── [lrwxrwxrwx root     root    ]  ./network-scripts/ifdown-isdn -> ifdown-ippp
├── [-rw-r--r-- root     root    ]  ./nftables.conf
├── [drwxr-xr-x root     root    ]  ./rhn
│   ├── [drwxr-xr-x root     root    ]  ./rhn/allowed-actions
│   │   ├── [drwxr-xr-x root     root    ]  ./rhn/allowed-actions/configfiles
│   │   └── [drwxr-xr-x root     root    ]  ./rhn/allowed-actions/script
│   ├── [drwxr-xr-x root     root    ]  ./rhn/clientCaps.d
│   └── [-rw-r--r-- root     root    ]  ./rhn/up2date
├── [-rw-r--r-- root     root    ]  ./rsyslog
├── [-rw-r--r-- root     root    ]  ./run-parts
├── [lrwxrwxrwx root     root    ]  ./selinux -> ../selinux/config
└── [-rw-r----- root     root    ]  ./sshd

👉 Ví dụ 8: Chúng ta có thể xem kích thước của mỗi file theo byte. Để in kích thước của mỗi file sử dụng tùy chọn -h và chỉ định ký tự kích thước kilobyte (K), megabyte (M), gigabyte (G), terabyte (T),...

[root@blogd-net-lab01 sysconfig]# tree -f -h
.
├── [  255]  ./anaconda
├── [   46]  ./chronyd
├── [    6]  ./console
├── [  150]  ./cpupower
├── [  110]  ./crond
├── [  417]  ./ebtables-config
├── [   73]  ./firewalld
├── [   15]  ./grub -> ../default/grub
├── [ 2.1K]  ./ip6tables-config
├── [ 2.1K]  ./iptables-config
├── [  903]  ./irqbalance
├── [ 1.7K]  ./kdump
├── [  185]  ./kernel
├── [  310]  ./man-db
├── [    6]  ./modules
├── [   22]  ./network
├── [ 4.0K]  ./network-scripts
│   ├── [  298]  ./network-scripts/ifcfg-ens192
│   ├── [  254]  ./network-scripts/ifcfg-lo
│   ├── [ 2.1K]  ./network-scripts/ifdown
│   ├── [  646]  ./network-scripts/ifdown-bnep
│   ├── [ 6.3K]  ./network-scripts/ifdown-eth
│   ├── [  769]  ./network-scripts/ifdown-ippp
│   ├── [ 4.4K]  ./network-scripts/ifdown-ipv6
│   ├── [   11]  ./network-scripts/ifdown-isdn -> ifdown-ippp
├── [  507]  ./nftables.conf
├── [   64]  ./rhn
│   ├── [   39]  ./rhn/allowed-actions
│   │   ├── [    6]  ./rhn/allowed-actions/configfiles
│   │   └── [    6]  ./rhn/allowed-actions/script
│   ├── [    6]  ./rhn/clientCaps.d
│   └── [ 1.8K]  ./rhn/up2date
├── [  196]  ./rsyslog
├── [    0]  ./run-parts
├── [   17]  ./selinux -> ../selinux/config
└── [  434]  ./sshd

👉 Ví dụ 9: Để hiển thị ngày của thời gian sửa đổi cuối cùng cho mỗi file thì chúng ta sử dụng tùy chọn -D như sau:

[root@blogd-net-lab01 sysconfig]# tree -f -pug -h -D
.
├── [-rw-r--r-- root     root       255 Nov  6  3:17]  ./anaconda
├── [-rw-r--r-- root     root        46 May 11  2019]  ./chronyd
├── [drwxr-xr-x root     root         6 Oct  1  9:40]  ./console
├── [-rw-r--r-- root     root       150 Sep 24  7:42]  ./cpupower
├── [-rw-r--r-- root     root       110 May 11  2019]  ./crond
├── [-rw------- root     root       417 Jul  1 11:41]  ./ebtables-config
├── [-rw-r--r-- root     root        73 May 11  2019]  ./firewalld
├── [lrwxrwxrwx root     root        15 Oct  2 13:00]  ./grub -> ../default/grub
├── [-rw------- root     root      2.1K Jul  1 11:41]  ./ip6tables-config
├── [-rw------- root     root      2.1K Jul  1 11:41]  ./iptables-config
├── [-rw-r--r-- root     root       903 May 11  2019]  ./irqbalance
├── [-rw-r--r-- root     root      1.7K May 11  2019]  ./kdump
├── [-rw-r--r-- root     root       185 Nov  6  3:16]  ./kernel
├── [-rw-r--r-- root     root       310 May 11  2019]  ./man-db
├── [drwxr-xr-x root     root         6 Oct  1  9:40]  ./modules
├── [-rw-r--r-- root     root        22 Nov  6  3:16]  ./network
├── [drwxr-xr-x root     root      4.0K Dec  8 10:55]  ./network-scripts
│   ├── [-rw-r--r-- root     root       298 Nov  6  3:16]  ./network-scripts/ifcfg-ens192
│   ├── [-rw-r--r-- root     root       254 Aug  3  2018]  ./network-scripts/ifcfg-lo
│   ├── [-rwxr-xr-x root     root      2.1K Aug  3  2018]  ./network-scripts/ifdown
│   ├── [-rwxr-xr-x root     root       646 Aug  3  2018]  ./network-scripts/ifdown-bnep
│   ├── [-rwxr-xr-x root     root      6.3K Aug  3  2018]  ./network-scripts/ifdown-eth
│   ├── [-rwxr-xr-x root     root       769 Aug  3  2018]  ./network-scripts/ifdown-ippp
│   ├── [-rwxr-xr-x root     root      4.4K Aug  3  2018]  ./network-scripts/ifdown-ipv6
│   ├── [lrwxrwxrwx root     root        11 Oct  1  9:40]  ./network-scripts/ifdown-isdn -> ifdown-ippp
├── [-rw-r--r-- root     root       507 May 11  2019]  ./nftables.conf
├── [drwxr-xr-x root     root        64 Nov  6  3:14]  ./rhn
│   ├── [drwxr-xr-x root     root        39 Nov  6  3:14]  ./rhn/allowed-actions
│   │   ├── [drwxr-xr-x root     root         6 Aug  4 21:20]  ./rhn/allowed-actions/configfiles
│   │   └── [drwxr-xr-x root     root         6 Aug  4 21:20]  ./rhn/allowed-actions/script
│   ├── [drwxr-xr-x root     root         6 Aug  4 21:20]  ./rhn/clientCaps.d
│   └── [-rw-r--r-- root     root      1.8K Feb 19  2018]  ./rhn/up2date
├── [-rw-r--r-- root     root       196 May 14  2019]  ./rsyslog
├── [-rw-r--r-- root     root         0 May 11  2019]  ./run-parts
├── [lrwxrwxrwx root     root        17 Nov  6  3:13]  ./selinux -> ../selinux/config
└── [-rw-r----- root     root       434 May 11  2019]  ./sshd

👉 Ví dụ 10: Một nội dung khá quan trọng đó là chúng ta có thể gửi hoặc chuyển hướng đầu ra của lệnh tree vào một tệp để phân tích sau bằng tùy chọn -o như bên dưới:

[root@blogd-net-lab01 sysconfig]# tree -o test.txt
[root@blogd-net-lab01 sysconfig]# cat test.txt
.
├── anaconda
├── chronyd
├── console
├── cpupower
├── crond
├── ebtables-config
├── firewalld
├── grub -> ../default/grub
├── ip6tables-config
├── iptables-config
├── irqbalance
├── kdump
├── kernel
├── man-db
├── modules
├── network
├── network-scripts
│   ├── ifcfg-ens192
│   ├── ifcfg-lo
│   ├── ifdown
│   ├── ifdown-bnep
│   ├── ifdown-eth
│   ├── ifdown-ippp
│   ├── ifdown-ipv6
│   ├── ifdown-isdn -> ifdown-ippp
├── nftables.conf
├── rhn
│   ├── allowed-actions
│   │   ├── configfiles
│   │   └── script
│   ├── clientCaps.d
│   └── up2date
├── rsyslog
├── run-parts
├── selinux -> ../selinux/config
├── sshd
└── test.txt

4. Lời kết

Qua bài trên, giúp cho chúng ta biết cách sử dụng lệnh tree trên hệ điều hành Linux. Từ đây giúp cho chúng ta có thể quản lý thư mục chúng ta một cách để dàng.