Ubuntu之tree命令

一般而言,ls 只是一个简单的 list 工具(虽然可以用各种参数组合来达到一些复杂的功能),但如果想要查看一个目录的目录结构就力不从心了,需要一种打印目录树的 tree 工具。

一段简短而强大的代码

很早以前,在网上看到一段简短而强大的代码,最终可以达到下面这种效果:

~tree .local

/home/dem/.local
   |-share
   |---applications
   |---desktop-directories

~ cd apps/firefox
~$ tree

/home/dem/apps/firefox
   |-chrome
   |---icons
   |-----default
   |-components
   |-defaults
   |---autoconfig
   |---pref
   |---profile
   |-----chrome
   |-extensions
   |---{972ce4c6-7e08-474-a285-320298ce6fd}   
   |---inspector~mozilla.org
   |-----chrome
   |-----components
   |-----defaults
   |-------preferences
   |---talkback~mozilla.org
   |-----components
   |-------talkback
   |-greprefs
   |-icons
   |-plugins
   |-res
   |---dtd
   |---entityTables
   |---fonts
   |---html
   |-searchplugins
   |-updates
   |---0

代码如下(下载链接):

#!/bin/sh
#######################################################
#  UNIX TREE                                          #
#  Version: 2.3                                       #
#  File: ~/apps/tree/tree.sh                          #
#                                                     #
#  Displays Structure of Directory Hierarchy          #
#  -------------------------------------------------  #
#  This tiny script uses "ls", "grep", and "sed"      #
#  in a single command to show the nesting of         #
#  sub-directories.  The setup command for PATH       #
#  works with the Bash shell (the Mac OS X default).  #
#                                                     #
#  Setup:                                             #
#     cd ~/apps/tree                                #
# chmod u+x tree.sh                             #
#     ln -s ~/apps/tree/tree.sh ~/bin/tree          #
# echo "PATH=~/bin:\{PATH}" >> ~/.profile      #
#                                                     #
#  Usage:                                             #
# tree [directory]                              #
#                                                     #
#  Examples:                                          #
#     tree                                          #
# tree /etc/opt                                 #
#     tree ..                                       #
#                                                     #
#  Public Domain Software -- Free to Use as You Like  #
#  http://www.centerkey.com/tree  -  By Dem Pilafian  #
#######################################################

echo
if [ "1" != "" ]  #if parameter exists, use as base folder
   then cd "1"
   fi
pwd
ls -R | grep ":" |   \
   sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/   /' -e 's/-/|/'
# 1st sed: remove colons
# 2nd sed: replace higher level folder names with dashes
# 3rd sed: indent graph three spaces
# 4th sed: replace first dash with a vertical bar
if [ `ls -F -1 | grep "/" | wc -l` = 0 ]   # check if no folders
   then echo "   -> no sub-directories"
   fi
echo
exit

仓库里的 tree 工具

因为前段时间使用 fedora,在没有上面那段脚本的情况下,敲入 tree 命令时却有输出,于是在 Ubuntu 下 apt-get install tree 了一下,果然有这个软件,而且功能很强大。

想想也是,上面那段代码有说 “Unix Tree / Linux Tree”,那自然是模拟实现已有程序的功能。

参数较多,man 一下或看在线版,比如可以用 L 参数来设定遍历层数,用 C 参数来使用颜色标识,使用 d 参数来只看目录。

2条评论

评论已关闭。