统计文件与文件夹大小信息的shell脚本
时间:2014-09-30 21:56 来源:linux.it.net.cn 作者:it
分享一个shell脚本,可用于统计文件与文件夹的大小。
统计文件/文件夹大小信息,参数如下:
-d 仅显示文件夹信息
-f 仅显示文件信息
-a 统计包括隐藏文件
-A 统计全部信息
代码:
复制代码代码示例:
#!/bin/bash
#Filename:f_count.sh
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
(责任编辑:IT)
分享一个shell脚本,可用于统计文件与文件夹的大小。
统计文件/文件夹大小信息,参数如下:
代码:
复制代码代码示例:
#!/bin/bash
#Filename:f_count.sh 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 |