Linux command: tree

Olarik Surinta
olarik
Published in
4 min readFeb 12, 2019

tree เป็นคำสั่งที่ใช้แสดงรายการของไดเรกทอรี (Directory) และไฟล์ (File) โดยเป็นการแสดงข้อมูลจากไดเรกทอรีปัจจุบัน

การติดตั้งโปรแกรม tree

  • การใช้คำสั่ง tree ทำได้โดยพิมพ์คำสั่ง ดังตัวอย่างต่อไปนี้
$ tree

แต่หากพิมพ์คำสั่งไปแล้วได้รับข้อความกลับมาจากระดับดังต่อไปนี้ แสดงว่ายังไม่ได้ติดตั้งโปรแกรม tree จึงไม่สามารถเรียกใช้คำสั่งได้

The program 'tree' is currently not installed. You can install it by typing:
sudo apt install tree
  • การติดตั้งโปรแกรม tree สามารถทำได้โดยพิมพ์คำสั่งดังต่อไปนี้
$ sudo apt install tree
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
tree
0 upgraded, 1 newly installed, 0 to remove and 2 not upgraded.
Need to get 40.7 kB of archives.
After this operation, 105 kB of additional disk space will be used.
Get:1 http://th.archive.ubuntu.com/ubuntu artful/universe amd64 tree amd64 1.7.0-5 [40.7 kB]
Fetched 40.7 kB in 0s (167 kB/s)
Selecting previously unselected package tree.
(Reading database ... 221222 files and directories currently installed.)
Preparing to unpack .../tree_1.7.0-5_amd64.deb ...
Unpacking tree (1.7.0-5) ...
Setting up tree (1.7.0-5) ...
Processing triggers for man-db (2.7.6.1-2) ...
  • หากติดตั้งโปรแกรมเสร็จสิ้น สามารถพิมพ์คำสั่ง tree เพื่อทดสอบการทำงาน ดังต่อไปนี้
$ tree
.
├── core
├── Desktop
│ └── ws
│ └── cpu-usage
│ ├── cpu.sh
│ ├── cpu_usage.sh
│ ├── cpu_usage.sh.save
│ ├── microgear-ZKFCxN5skXCGqsG.cache
│ ├── netpie_cpu_usage.py
│ └── test.py
├── Documents
├── Downloads
│ └── google-chrome-stable_current_amd64.deb
├── examples.desktop
├── jcameron-key.asc
├── Music
├── Pictures
├── Public
├── PycharmProjects
│ └── untitled
│ └── venv
│ ├── bin
│ │ ├── activate
│ │ ├── activate.csh
│ │ ├── activate.fish
│ │ ├── easy_install
│ │ ├── easy_install-3.6
│ │ ├── pip
│ │ ├── pip3
│ │ ├── pip3.6
│ │ ├── python
│ │ ├── python3
│ │ └── python3.6
│ ├── include
│ ├── lib
│ │ └── python3.6
│ │ └── site-packages
│ │ ├── easy-install.pth
│ │ ├── pip-9.0.1-py3.6.egg
│ │ │ ├── EGG-INFO

จากตัวอย่างข้างต้น ไดเรกทอรีปัจจุบันคือ root directory จึงทำให้แสดงรายการของไดเรกทอรีและไฟล์ออกมาเป็นจำนวนมาก

  • ตัวอย่างต่อไปนี้แสดงรายการไดเรกทอรีและไฟล์ทั้งหมดจากไดเรกทอรี Desktop
kaveepoj@ProLiantMicroServer:~$ cd Desktop/
kaveepoj@ProLiantMicroServer:~/Desktop$
kaveepoj@ProLiantMicroServer:~/Desktop$ tree
.
└── ws
└── cpu-usage
├── cpu.sh
├── cpu_usage.sh
├── cpu_usage.sh.save
├── microgear-ZKFCxN5skXCGqsG.cache
├── netpie_cpu_usage.py
└── test.py

จากตัวอย่างข้างต้น เข้าไปยังไดเรกทอรี Desktop โดยการพิมพ์คำสั่ง cd และตามด้วยชื่อไดเรกทอรี Desktop ก็จะทำให้เข้าไปยังไดเรกทอรี Desktop ได้

ตัวเลือกของคำสั่ง tree

ในคำสั่ง tree หากต้องการดูตัวเลือก (Option) ทั้งหมดสามารถทำได้โดยพิมพ์คำสั่ง ดังต่อไปนี้

$ tree --help
usage: tree [-acdfghilnpqrstuvxACDFJQNSUX] [-H baseHREF] [-T title ]
[-L level [-R]] [-P pattern] [-I pattern] [-o filename] [--version]
[--help] [--inodes] [--device] [--noreport] [--nolinks] [--dirsfirst]
[--charset charset] [--filelimit[=]#] [--si] [--timefmt[=]<f>]
[--sort[=]<name>] [--matchdirs] [--ignore-case] [--] [<directory list>]
------- Listing options -------
-a All files are listed.
-d List directories only.
-l Follow symbolic links like directories.
-f Print the full path prefix for each file.
-x Stay on current filesystem only.
-L level Descend only level directories deep.
-R Rerun tree when max dir level reached.
-P pattern List only those files that match the pattern given.
-I pattern Do not list files that match the given pattern.
--ignore-case Ignore case when pattern matching.
--matchdirs Include directory names in -P pattern matching.
--noreport Turn off file/directory count at end of tree listing.
--charset X Use charset X for terminal/HTML and indentation line output.
--filelimit # Do not descend dirs with more than # files in them.
--timefmt <f> Print and format time according to the format <f>.
-o filename Output to file instead of stdout.
-------- File options ---------
-q Print non-printable characters as '?'.
-N Print non-printable characters as is.
-Q Quote filenames with double quotes.
-p Print the protections for each file.
-u Displays file owner or UID number.
-g Displays file group owner or GID number.
-s Print the size in bytes of each file.
-h Print the size in a more human readable way.
--si Like -h, but use in SI units (powers of 1000).
-D Print the date of last modification or (-c) status change.
-F Appends '/', '=', '*', '@', '|' or '>' as per ls -F.
--inodes Print inode number of each file.
--device Print device ID number to which each file belongs.
------- Sorting options -------
-v Sort files alphanumerically by version.
-t Sort files by last modification time.
-c Sort files by last status change time.
-U Leave files unsorted.
-r Reverse the order of the sort.
--dirsfirst List directories before files (-U disables).
--sort X Select sort: name,version,size,mtime,ctime.
------- Graphics options ------
-i Don't print indentation lines.
-A Print ANSI lines graphic indentation lines.
-S Print with CP437 (console) graphics indentation lines.
-n Turn colorization off always (-C overrides).
-C Turn colorization on always.
------- XML/HTML/JSON options -------
-X Prints out an XML representation of the tree.
-J Prints out an JSON representation of the tree.
-H baseHREF Prints out HTML format with baseHREF as top directory.
-T string Replace the default HTML title and H1 header with string.
--nolinks Turn off hyperlinks in HTML output.
---- Miscellaneous options ----
--version Print version and exit.
--help Print usage and this help message and exit.
-- Options processing terminator.
  • ตัวอย่างของการตรวจสอบเวอร์ชันของโปรแกรม tree สามารถทำได้ดังนี้
$ tree --version
tree v1.7.0 (c) 1996 - 2014 by Steve Baker, Thomas Moore, Francesc Rocher, Florian Sesser, Kyosuke Tokoro

--

--