Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2378518
  • 博文数量: 298
  • 博客积分: 7876
  • 博客等级: 准将
  • 技术积分: 5500
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-23 13:39
文章存档

2013年(2)

2012年(142)

2011年(154)

分类: Python/Ruby

2012-04-21 21:21:43

统计文件/文件夹大小信息

 

该脚本用于统计文件/文件夹大小信息

-d 仅显示文件夹信息

-f 仅显示文件信息

-a 统计包括隐藏文件

-A 统计全部信息

 

#!/bin/bash

 

usage()

{

     cat <<-EOF

---------------------------------------------------------

该脚本用于统计文件/文件夹大小信息

-d 仅显示文件夹信息

-f 仅显示文件信息

-a 统计包括隐藏文件

-A 统计全部信息

---------------------------------------------------------

EOF

}

 

caldir(){

 

total1=0

total2=0

exern=

[ $ALL ] && exern="-a"

#find $1 -maxdepth 1 当前文件夹下

for file in `ls $exern $1`

do

     if [ "$file" = "." ];then #去掉 .
        continue
     fi
     if [ "$file" = ".." ];then #去掉 ..
        continue
     fi

     path="$1/$file"

     size=`stat -c %s $path`

     if [ -f $path ];then

         ((total1 =total1 + size))

         [ $FILE ] && echo "$path File Size:$size"

         size=0

     elif [ -d $path ];then

         ((total2 = total2 + size))

         [ $DIR ] && echo "$path Dir Size:$size"

         size=0

     fi

done

[ $FILE ] && echo "$1 Total File Size: $total1 bytes"

[ $DIR ] && echo "$1 Total Dir Size: $total2 bytes"

}

FILE=1

DIR=

ALL=

while getopts dfaA opt

do

     case $opt in

     d)   DIR=1

         FILE=

         ;;

     f)   DIR=

         FILE=1

         ;;

     a)   ALL=1

         ;;

     A)   DIR=1

         FILE=1

         ALL=1

         ;;

     ?)   usage

         ;;

     esac

done

shift $((OPTIND - 1))

for one in "$@"

do

     if [ -f $one ];then

         stat $one

     elif [ -d $one ];then

         caldir $one

     else

         echo "$one is not file and dir."

     fi

done

 

阅读(2047) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~