[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]#