1.14 统计某个目录下的文件类和数量

思路:

  1. 如何处理需要输入的目标目录,也就是要进行统计查询的对象
  2. 如何判断文件类型并获取文件类型关键字
  3. 如何根据将文件类型与文件数量进行挂钩
  4. 如何在将文件类型和文件数量挂钩后进行文件数量的计算
    5)如何批量处理文件
    6)如何输出文件类型和相应的数量
  5. 全局变量设置
  6. 汇总脚本

下面一层层来解决

  1. 如何处理需要输入的目标目录,也就是要进行统计查询的对象
[ $# -ne 1 ] && { echo "Usage: $(basename $0) need one argument"}   
dir=$1
  1. 如何判断文件类型并获取文件类型关键字
    file -b filename | cut -d, -f1

  2. 如何根据将文件类型与文件数量进行挂钩

ftype=$(file -b filename | cut -d, -f1)   
# 利用关联数组来实现,文件类型作为数组索引,数组元素的值作为文件统计的数量   
# 先定义一个数组   
declare -A filearray  
  1. 如何在将文件类型和文件数量挂钩后进行文件数量的计算 ((filearray["$ftype"]+=1))

  2. 如何批量处理文件

while read line
do
ftype=$(file -b $line | cut -d, -f1)   
((filearray["$ftype"]+=1))
done < <(find $dir -type f -print)
  1. 如何输出文件类型和相应的数量
for ftype in "${!filearray[@]}"
do
echo $ftype ${filearray["$ftype"]}
done
  1. 全局变量设置
    declare -A filearray

  2. 汇总脚本

#!/bin/bash

findfile () {
while read line
do
ftype=$(file -b $line | cut -d, -f1)   
((filearray["$ftype"]+=1))
done < <(find $dir -type f -print)

for ftype in "${!filearray[@]}"
do
echo $ftype ${filearray["$ftype"]}
done
}

[ $# -ne 1 ] && { echo "Usage: $(basename $0) need one argument";exit -1; }   
dir=$1
declare -A filearray

findfile

ps:脚本中用文件类型ftype作数组索引,每个索引对应的值是该类型文件的数量

上一页➡️

下一页➡️

Calendar Oct 31, 2023
Edit Edit this page
本站总访问量:  次 您是本站第  位访问者