首页 > 学技术 > 技术网文 > Linux > 正文

[原创] [分享] 一个改进版的 tree 小脚本 :xtree (已增加注释)


来源 chinaunix.net 酷勤网整理

[color=blue]
**************************************************************************************
注 :该文章参考了如下内容 :

A)man tree

作者 :ailms <ailms{@}qq{dot}com>

版本 :v1.3

最后修改 :2007/07/03 10:10 :增加注释
**************************************************************************************
[/color]

 tree 命令相信大家都用过,就象 dos 中的 tree 命令,把目录和其下的内容按树型结构“画”出来。

相比 ls -R 的方式,tree 的输出比较便于查看。而且 tree 命令支持 -s (显示大小)、-u/-g(显示 user/group)

和 -D (last change time)。 不过 tree 命令有几个方面我感觉不太好 :

 1) -s 是已字节显示的,对于大文件来说不太直观; 
 
 2) -D 的输出格式也不是很直观。是象 “May 30 10:43” 这样的外国风格,所以想改为 ‘YYYY-MM-DD hh:mm’ 的风格

 3) tree 不能象 find 命令一样指定最大的递归深度,有时我只想看大概的内容,而不是全部的内容,这个时候如果能够限制“显示的深度”就行了。

这三个问题就是该脚本想解决的。

[color=red]注 :该脚本不 follow symbol-link [/color]

下面是截图


[color=blue] [size=3]默认的输出[/size][/color]



[color=blue][size=3]下面两张是指定 max-depth 的输出[/size][/color]






下面是源代码




[root@mail filesystems]# cat mytree.sh |grep -v '^$'
#!/bin/bash
# Description : This script is used to change the output of commadn 'tree -ugDs'
# to more human-readable,about two things :
# 1. change the unit of '-s' from Byte to KB
# 2. change the fomat of '-D' to "YYYY-MM-DD hh:mm"
# 3. you can set the max depth which will be output"
alias tree='tree -Ds'
target_dir=$1
max_level=$2
if [ "$1" == '-h' ];then
        echo "Usage : xtree [target_dir] [max_depth]"
        exit 0
fi
# If target_dir is null,default to $(pwd),or check the availability 
if [ -z "$target_dir" ];then
        target_dir=$(pwd)
elif [ ! -x "$target_dir" ] || [ ! -r "$target_dir" ];then
        # Permission Denied
        echo "ERROR! Permission Denied"
        exit 1
elif [ ! -e "$target_dir" ];then
        # Directory invalid
        echo
        echo "ERROR ! < $target_dir > does NOT exist ..."
        exit 1
fi 
# If max_level is 'null' ,default to 0 (unlimit)
if [ -z "$max_level" ];then
        leading_space=0
        max_level=0
elif [ $max_level -lt 0 ];then

        leading_space=0
else
        # get the leading space counts for layer <max_level>
        leading_space=$(( 4 * max_level - 4 ))
fi
# Print the title bar
echo
echo "-----------------------------------------------------------------------"
echo " NOTE :  / [ Directory ]   * [ Excutable ]   = [ Socket ]   | [ FIFO  ]"
echo "-----------------------------------------------------------------------"
echo
# Main body
# 1) exec the 'tree -CFDsgu' command and extract the main body of output
# 2) use 'sed' to split the output line into several fileds,and mark the third(size) and furth(change-time) fileds
# 3) use 'awk' FS=@ OFMT="%.2f" to change the thrid filed(size) into KB,the furth(change-time) will be modify latter
tree -CFDsgu "$target_dir" |tail -n +2 |egrep -v '^([[:digit:]] directories,)'|egrep -v '^$' \
 | sed  "s/\[\([[:alpha:]]\+\)* *\([[:alpha:]]\+\)* *\([[:digit:]]\+\)\(.\+\)*] *\(.\+\)/[\1 \2 @\3@\4@] \5/" \
 | awk '{print $1,"@",$2 /1024,"KB","@",$3,"@",$4}'  FS=@ OFMT="%.2f"  > /tmp/tmp.out
# output everythinig except the (change-time) filed ,please note that 'Filed seperator (fs)' is  '@'
awk '{print $1,$2,"@",$4,"@"}' FS=@ /tmp/tmp.out  > /tmp/a
# output the change-time filed and translate into 'YYYY-MM-DD hh:mm' by the pipe ,and output to /tmp/b
awk '{print $3 |"xargs -i date -d {} \"+%Y-%m-%d %H:%M\""}' FS=@ /tmp/tmp.out > /tmp/b
# use paste to join the two files
paste -d '' /tmp/a /tmp/b > /tmp/c
echo "MAX-Level ==> $max_level"
echo
# Because we filter out the first line and  last line of 'tree -CFDugs' 's output above,so we print the first line
echo "$target_dir"
if [ $max_level -gt 0 ];then    # if we specified the max-level 
        awk '{print $1$3,$2}' FS=@ /tmp/c |egrep -v "^ {$leading_space,}" |tee /tmp/d
else    # if we did NOT specify the max-level
        awk '{print $1$3,$2}' FS=@ /tmp/c |tee /tmp/d
fi
# count the directories and files 
dir_cnt=$(egrep -c '/ *$' /tmp/d)
file_cnt=$(egrep -vc '/ *$' /tmp/d)
echo
# output the statistics line
echo "$dir_cnt directories, $file_cnt files"
unalias tree
# remove temporary files
rm -f /tmp/tmp.out /tmp/[abcd]
echo
# END
[root@mail filesystems]# 



[ 本帖最后由 ailms 于 2007-7-3 10:40 编辑 ]



 寂寞烈火 回复于:2007-07-03 02:35:25

VeryCooooooool :em02:


 
ailms 回复于:2007-07-03 03:10:19

不好意思,原脚本有一处 bug ,现已改正,第1张截图已更新


 
woody1980 回复于:2007-07-03 14:02:49

虽然看不动,一样要收藏...............




原文链接:http://linux.chinaunix.net/bbs/viewthread.php?tid=902536
转载请注明作者名及原文出处



收藏本页到: