LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 985|回复: 8

yet another tree.sh

[复制链接]
发表于 2003-6-13 23:30:03 | 显示全部楼层 |阅读模式
#!/bin/sh

# glob variable.
branch="|"
showmsg=""

# The function display a file or directory as a branch.
display_file(){
        echo -e $showmsg$branch"-----$1"
}

# The function searchs directory.
search_dir(){
        cd $1
        local dirlist
        eval "dirlist=($(dir .))" # dirlist is a array.
       
        local c=${#dirlist[@]} # The c variable must be local.
        for dirent in ${dirlist[@]}; do
                if (( $c == 1 )); then
                        branch='\' # change the end of branch.
                fi

                display_file $dirent
                branch="|" # restore | into the branch variable.
               
                if [ -d $dirent ] ;then
                        showmsg=$showmsg"|\t"
                       
                        if (( $c == 1 )); then
                                showmsg=${showmsg%"|\t"}"\t"
                        fi
                        search_dir $dirent
                fi
                let c--
        done
       
        showmsg=${showmsg%"\t"}; showmsg=${showmsg%|}
        cd ..
}

# # # # # # # # #
# main program. #
# # # # # # # # #

if (( $# > 1 )); then
        echo "Usage: $0 [ directory-name ]"
        exit 1
fi
search_dir $1

这个版本比较实用,除了些bug。
你可以自己加些功能,如指定显示n级目录。
:cool:
发表于 2003-6-13 23:55:45 | 显示全部楼层
我也来一个
这个出自Advanced Bash-Scripting Guide

#!/bin/sh
#         @(#) tree      1.1  30/11/95       by Jordi Sanfeliu
#                                         email: mikaku@fiwix.org
#
#         Initial version:  1.0  30/11/95
#         Next version   :  1.1  24/02/97   Now, with symbolic links
#         Patch by       :  Ian Kjos, to support unsearchable dirs
#                           email: beth13@mail.utexas.edu
#
#         Tree is a tool for view the directory tree (obvious :-) )
#

# ==> 'Tree' script used here with the permission of its author, Jordi Sanfeliu.
# ==> Comments added by the author of this document.
# ==> Argument quoting added.


search () {
   for dir in `echo *`
   # ==> `echo *` lists all the files in current working directory, without line breaks.
   # ==> Similar effect to     for dir in *
   # ==> but "dir in `echo *`" will not handle filenames with blanks.
   do
      if [ -d "$dir" ] ; then   # ==> If it is a directory (-d)...
         zz=0   # ==> Temp variable, keeping track of directory level.
         while [ $zz != $deep ]    # Keep track of inner nested loop.
         do
            echo -n "|   "    # ==> Display vertical connector symbol,
                              # ==> with 2 spaces & no line feed in order to indent.
            zz=`expr $zz + 1` # ==> Increment zz.
         done
         if [ -L "$dir" ] ; then   # ==> If directory is a symbolic link...
            echo "+---$dir" `ls -l $dir | sed 's/^.*'$dir' //'`
            # ==> Display horiz. connector and list directory name, but...
            # ==> delete date/time part of long listing.
         else
            echo "+---$dir"      # ==> Display horizontal connector symbol...
                                 # ==> and print directory name.
            if cd "$dir" ; then  # ==> If can move to subdirectory...
               deep=`expr $deep + 1`   # ==> Increment depth.
               search     # with recursivity ;-)
                          # ==> Function calls itself.
               numdirs=`expr $numdirs + 1`   # ==> Increment directory count.
            fi
         fi
      fi
   done
   cd ..   # ==> Up one directory level.
   if [ "$deep" ] ; then  # ==> If depth = 0 (returns TRUE)...
      swfi=1              # ==> set flag showing that search is done.
   fi
   deep=`expr $deep - 1`  # ==> Decrement depth.
}

# - Main -
if [ $# = 0 ] ; then
   cd `pwd`    # ==> No args to script, then use current working directory.
else
   cd $1       # ==> Otherwise, move to indicated directory.
fi
echo "Initial directory = `pwd`"
swfi=0      # ==> Search finished flag.
deep=0      # ==> Depth of listing.
numdirs=0
zz=0

while [ "$swfi" != 1 ]   # While flag not set...
do
   search   # ==> Call function after initializing variables.
done
echo "Total directories = $numdirs"

exit 0
发表于 2003-6-15 13:30:10 | 显示全部楼层
我也来一个;)
这个出自www.chinaunix.net
#!/bin/ksh
#
dir=${1:-.}
(cd $dir;pwd)
find $dir -type d -print |du | awk '{print $2, "== ("$1/2"kb)"}' |sort -f | sed -e "s,[^ /]*/\([^ /]*\) ==,\|--\1," -e"s,[^ /]*/,| ,g"
我想这个是最简单实用的吧~~
 楼主| 发表于 2003-6-16 23:14:38 | 显示全部楼层
楼上那个我试过。在我的机子上报错。
故此我自己写了一个。
这个短是短些,只是看起来有点头痛,所以也不想调试了。
发表于 2003-6-17 00:19:26 | 显示全部楼层
嗬~~,其实很简单,那个find都是多余的;)因为du本身就有对子目录进行统计的功能,(注意,下列是一行)
du|awk '{print $2, "== ("$1/2"kb)"}'|sort -f|sed -e "s,[^ /]*/\([^ /]*\) ==,\|--\1," -e"s,[^ /]*/,| ,g"
发表于 2005-2-21 13:56:35 | 显示全部楼层
Post by grampus

这个版本比较实用,除了些bug。
你可以自己加些功能,如指定显示n级目录。
:cool:


对于文件名中有空格的文件不能正确处理
回复 支持 反对

使用道具 举报

发表于 2005-2-22 17:37:19 | 显示全部楼层
在处理有空格的目录名有BUG
回复 支持 反对

使用道具 举报

发表于 2005-2-22 17:42:32 | 显示全部楼层
对有特殊字符‘()’有BUG
回复 支持 反对

使用道具 举报

发表于 2005-2-26 10:27:39 | 显示全部楼层
我也凑个热闹:
find后加入/防止目标是连接,同时统一pwd和$1的标准
原理很简单, 统计 "/" 的个数

  1. #! /bin/bash
  2. case $# in
  3. 0) curpath=$PWD ;;
  4. 1) curpath="$1";;
  5. *) echo "usage $0 [path]" ; exit 1 ;;
  6. esac
  7. find "$curpath/" | awk 'BEGIN {FS="/"}
  8.                     FNR == 1 { base = NF ; print $0}
  9.                     FNR >1 {
  10.                     for ( i = base ; i < NF; i++ )
  11.                         { printf("|    ")}
  12.                           print "|--- " $NF
  13.                      }'
复制代码
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表