Linux安裝tree指令

Linux安裝tree指令

情景描述:
有個場景想獲取文件夾的目錄結構,一個個寫真的很煩, 記得有個tree命令,能打印出命令,在機器上敲了一下tree,但是沒有反應,這不是Linux自帶的命令,就在網上找了一下安裝包和安裝方法
root用戶執行

root用戶執行

可以在這裏下載
http://www.linuxfromscratch.org/blfs/view/svn/general/tree.html

命令:
wget http://mama.indstate.edu/users/ice/tree/src/tree-1.8.0.tgz

解壓文件,進入文件,執行
make install
這樣就可以使用tree命令了

非root用戶執行

[activemq@localhost product]$  wget http://mama.indstate.edu/users/ice/tree/src/tree-1.8.0.tgz
--2019-11-14 14:39:23--  http://mama.indstate.edu/users/ice/tree/src/tree-1.8.0.tgz
......
2019-11-14 14:39:24 (99.7 KB/s) - ‘tree-1.8.0.tgz.1’ saved [50286/50286]
[activemq@localhost product]$ tar -zxvf tree-1.8.0.tgz
tree-1.8.0/CHANGES
......
tree-1.8.0/doc/xml.dtd
[activemq@localhost product]$ cd tree-1.8.0/
[activemq@localhost tree-1.8.0]$ vim Makefile 
#prefix = /usr
#將上面一句註釋掉,添加下面的路徑
prefix = /home/activemq/product/tree-1.8.0
[activemq@localhost tree-1.8.0]$ make install
gcc -ggdb -pedantic -Wall -DLINUX -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -c -o tree.o tree.c
......
install doc/tree.1 /home/activemq/product/tree-1.8.0/man/man1/tree.1

[activemq@localhost tree-1.8.0]$ cd 
[activemq@localhost ~]$ vi .bash_profile 
#添加
export PATH=/home/activemq/product/tree-1.8.0/bin:$PATH
[activemq@localhost ~]$ source .bash_profile 
[activemq@localhost ~]$ tree
#就有樹的展示了

tree

樹狀圖列出目錄的內容

補充說明

**tree命令** 以樹狀圖列出目錄的內容。

語法

tree(選項)(參數)

選項

------- 列表選項 -------
-a            # 顯示所有文件和目錄。
-d            # 先是目錄名稱而非文件。
-l            # 如遇到性質爲符號連接的目錄,直接列出該連接所指向的原始目錄。
-f            # 在每個文件或目錄之前,顯示完整的相對路徑名稱。
-x            # 將範圍侷限在現行的文件系統中,若指定目錄下的某些子目錄,其存放於另一個文件系統上,則將該目錄予以排除在尋找範圍外。
-L level      # 限制目錄顯示層級。
-R            # Rerun tree when max dir level reached.
-P pattern    # <範本樣式> 只顯示符合範本樣式的文件和目錄名稱。
-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.
-------- 文件選項 ---------
-q            # 用“?”號取代控制字符,列出文件和目錄名稱。
-N            # 直接列出文件和目錄名稱,包括控制字符。
-Q            # Quote filenames with double quotes.
-p            # 列出權限標示。
-u            # 列出文件或目錄的擁有者名稱,沒有對應的名稱時,則顯示用戶識別碼。
-g            # 列出文件或目錄的所屬羣組名稱,沒有對應的名稱時,則顯示羣組識別碼。
-s            # 列出文件和目錄大小。
-h            # Print the size in a more human readable way.
--si          # Like -h, but use in SI units (powers of 1000).
-D            # 列出文件或目錄的更改時間。
-F            # 在執行文件,目錄,Socket,符號連接,管道名稱名稱,各自加上"*","/","@","|"號。
--inodes      # Print inode number of each file.
--device      # Print device ID number to which each file belongs.
------- 排序選項 -------
-v            # Sort files alphanumerically by version.
-t            # 用文件和目錄的更改時間排序。
-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.
------- 圖形選項 ------
-i            # 不以階梯狀列出文件和目錄名稱。
-A            # 使用ASNI繪圖字符顯示樹狀圖而非以ASCII字符組合。
-S            # Print with CP437 (console) graphics indentation lines.
-n            # Turn colorization off always (-C overrides).
-C            # 在文件和目錄清單加上色彩,便於區分各種類型。
------- XML / HTML / JSON選項 -------
-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.
---- 雜項選項 ----
--version     # 輸入版本信息。
--help        # 打印使用幫助信息。
--            # Options processing terminator.

參數

目錄:執行tree指令,它會列出指定目錄下的所有文件,包括子目錄裏的文件。

實例

列出目錄/private/ 第一級文件名
tree  /private/ -L 1
/private/
├── etc
├── tftpboot
├── tmp
└── var

忽略文件夾

tree -I node_modules # 忽略當前目錄文件夾node_modules
tree -P node_modules # 列出當前目錄文件夾node_modules的目錄結構
tree -P node_modules -L 2 # 顯示目錄node_modules兩層的目錄樹結構
tree -L 2 > /home/www/tree.txt # 當前目錄結果存到 tree.txt 文件中

忽略多個文件夾

tree -I 'node_modules|icon|font' -L 2

參考:
https://blog.csdn.net/hizengbiao/article/details/89180491
https://wangchujiang.com/linux-command/c/tree.html

發佈了14 篇原創文章 · 獲贊 10 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章