统计svn指定的url下所有文件的总大小(不包括目录)


shell


  • Read me

    • 要求:Linux OS
    • 准备:将下面的代码放入文本文档并重命名文件为“count_svn_files_size.sh”
    • 使用:sh count_svn_files_size.sh YOUR_SVN_URL
  • 源码

#!/bin/bash

# 统计svn指定的url下所有文件的总大小,排除掉了目录
# 使用方法: sh count_svn_files_size.sh YOUR_SVN_URL
# 举例: sh count_svn_files_size.sh http://127.0.0.1/svn/abc/def 

# 指定的url目录
base_url=$1
# 用来临时存储svn目录的文件
temp_dir_record="temp_dir_record.log"
# 总目录数量
all_dir_num=0

# 判断传入的url是否为空
if [ "${base_url}" == "" ]; then
  echo "empty svn url"
  exit 1;
fi

# 删除临时文件,避免重复执行命令时收到干扰
rm -f ${temp_dir_record}

# 进度条方法: progress 1 100
# 参数1: 当前值
# 参数2: 总值
function progress () {
  pro=`awk 'BEGIN{printf "%d",('$1'/'$2')*100}'`
    spaces=`seq -s '#' $((${pro} +1)) | sed 's/[0-9]//g'`
    if [ "${pro}" != "100" ]; then
      printf "[%-100s] %d%% \r" "${spaces}" "${pro}"
    else
      printf "[%-100s] %d%% \n" "${spaces}" "${pro}"
    fi
}

# 递归统计指定的url下有多少目录,并缓存到临时文件中
function count_dir() {
  local url=$1
  echo "counting dir: ${url}"
  echo "${url}" >> ${temp_dir_record}
  local arr=`svn list $url | grep '\/' | grep -v '\.\/'`
  for d in ${arr}
  do
    d_url="${url}${d}"
    count_dir ${d_url}
  done
}

# 统计所有目录下的所有文件
function count_files() {
  local url=$1
  local files_size=`svn list -v ${url} | grep -v '\.\/' | grep -v '\/' | awk '{sum+=$3} END {print sum}'`
  if [ "${files_size}" == "" ]; then
    echo 0
  fi
  echo ${files_size}
}

# 统计目录
echo "start to count all dir"
starttime=`date +'%Y-%m-%d %H:%M:%S'`
count_dir "${base_url}/"
all_dir_num=`cat ${temp_dir_record} | wc -l`
echo "end to count all dir"
echo "all dir num is: ${all_dir_num}"
endtime=`date +'%Y-%m-%d %H:%M:%S'`
start_seconds=$(date --date="$starttime" +%s);
end_seconds=$(date --date="$endtime" +%s);
echo "it took "$((end_seconds-start_seconds))"s"

# 统计文件的空间占用
echo "start to count repository size"
total_size=0
progress_num=0
starttime=`date +'%Y-%m-%d %H:%M:%S'`
for line in `cat ${temp_dir_record}`
do
  size=$(count_files ${line})
  total_size=$((${total_size} + ${size}))
  progress_num=$((${progress_num} + 1))
  progress ${progress_num} ${all_dir_num}
done
echo "end to count all file size"
echo "total size is: ${total_size}Bytes"
endtime=`date +'%Y-%m-%d %H:%M:%S'`
start_seconds=$(date --date="$starttime" +%s);
end_seconds=$(date --date="$endtime" +%s);
echo "it took "$((end_seconds-start_seconds))"s"