uf123 发表于 2018-8-27 09:08:59

shell统计指定目录下所有文件类型及数量

  #!/bin/bash
  #Synopsis:用于统计脚本当前所在目录或者用户指定目录下的所有文件类型及数量
  #若直接运行脚本而不接任何命令行参数,则默认会统计脚本所在目录下的文件
  #Date:2016/10
  #Author:Jian
  #Usage:sh fileStat.sh /path1 /path2
  testFile=$(mktemp /tmp/testfile.XXX)
  #如果没有指定查询目录,则使用默认的当前脚本所在目录
  if [ $# -eq 0 ]; then
  path="$( cd "$( dirname "${BASH_SOURCE}" )" && pwd )/"
  else
  path="$@"
  fi
  #在给出的所有目录中循环查询
  for dir in $path
  do
  if [ ! -d $dir ]; then
  #若给出错误路径则用红色字体打印出来
  echo -e "\e \e[0m"
  continue
  fi
  #find命令递归查找指定目录下面所有文件及目录
  find $dir -mindepth 1 -print | while read line
  do
  ftype="$(file -b $line)"
  echo $ftype
  done>$testFile
  echo "==========Directory [$dir] File type and counts=========="
  #使用awk关联数组统计文件类型所对应的数目
  awk '
  { count[$0]++ }
  END{
  for(ftype in count)
  { printf ("%s: %d\n",ftype,count) }
  }' $testFile | sort
  #删除创建的临时文件
  rm -rf $testFile
  done


页: [1]
查看完整版本: shell统计指定目录下所有文件类型及数量