免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1091 | 回复: 0
打印 上一主题 下一主题

类似tree命令的脚本 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-10-25 14:22 |只看该作者 |倒序浏览
输出目录树:
一个输效果基本和tree命令的输出完全一样(除了目录未的工作不同),文件带色彩输出,文件、目录数统计,贴上来给大家做个参考:
tree.sh
------------------------------------------------------------
代码:
#!/bin/sh
#
# tree.sh
# A tool that display the dictionary structure in dos's
# tree command style.
#
#      __@
#  _ \
#
# Tested on slackware, openbsd, netbsd, freebsd.
#
# Just for fun.
#
# The name of the ls program, please use
# the absolute path, otherwise, there
# may be get some strange errors.
#
LSPROG="/bin/ls"
# COLOR DEFINE
# ============
#
DIR="\033[01;34m"
EXE="\033[01;32m"
DEV="\033[01;33m"
LNK="\033[01;36m"
ZIP="\033[01;31m"
SOCK="\033[01;35m"
NULL="\033[00m"
ROOT=${1:-.}
TRUE=0
FALSE=1
LAYERS=0
FILECOUNT=0
DIRCOUNT=0
# print_dash
# ==========
# Print the structure lines
#
print_dash()
{
local i=0
local num=$1
while [ $i -lt $num ]; do
  echo -n "|"
  for j in 1 2 3; do
   echo -n " "
  done
  i=`expr $i + 1`
done
echo -n "|-- "
}
# ispkg
# =====
# Test if the file is a package like:
# .gz .tar .tgz .tar.gz .zip .rar .rpm
# and etc.
#
ispkg()
{
local f=$1
local i
# Package extension list, you can add your coustom
# extensions in it.
#
local pkg__ext=".gz .tar .tgz .tar.gz .zip .rar .rpm"
# if the file's suffix contain any package extension
# then cut it.
for i in $pkg__ext; do
  f=${f%$i}
done
if [ "$f" != "$1" ]; then
  return $TRUE
else
  return $FALSE
fi
}
# mktree
# ======
# The main function, that print the
# dictionary structure in dos's tree
# command style. It's runs in nesting.
#
mktree()
{
local f
for f in `$LSPROG -1 $1 2> /dev/null`; do
  f=${f%/}
  f=${f##*/}
  # If dictionary then print it and enter
  # the nesting block.
  if [ -d $1/$f ]; then
   print_dash $LAYERS
   echo -e "${DIR}$f${NULL}"
   DIRCOUNT=`expr $DIRCOUNT + 1`
   LAYERS=`expr $LAYERS + 1`
   mktree $1/$f
  else
   print_dash $LAYERS
   # file is a symbol link
   if [ -L $1/$f ]; then
    echo -e "${LNK}$f${NULL}"
   # file is executable
   elif [ -x $1/$f ]; then
    echo -e "${EXE}$f${NULL}"
   # file is a device
   elif [ -c $1/$f -o -b $1/$f ]; then
    echo -e "${DEV}$f${NULL}"
   # file is a socket
   elif [ -S $1/$f ]; then
    echo -e "${SOCK}$f${NULL}"
   # file is a package
   elif `ispkg $f`; then
    echo -e "${ZIP}$f${NULL}"
   else
    echo -e "$f"
   fi
   
   FILECOUNT=`expr $FILECOUNT + 1`
  fi
done
LAYERS=`expr $LAYERS - 1`
}
echo $ROOT
mktree $ROOT
echo "\`"
echo "$DIRCOUNT directories, $FILECOUNT files"
显示效果:
/home/matthew/
|--src
|   |--asm
|   |   |-- hello.s
|   |   |-- shell.s
|   |   |--hello
|   |   |--shell
|   |--shell
|   |   |--tree.sh
|--xfce.tar.gz
|-- mbox
`
3 directories, 7 files



文章出处:
http://www.diybl.com/course/3_program/shell/shelljs/200862/119781.html


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/81965/showart_1335087.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP