linux shell 统计不同类型文件数目

来源:互联网 发布:电子版报纸制作软件 编辑:程序博客网 时间:2024/06/05 17:15
文件类型可以由ls –l中的第一列获得,为了确保不漏掉文件,可以加上-a参数
dir = ~
names=$(ls –la ${dir})
ls –l的输出中,第一列第一个字符指示该条目是文件、链接还是目录,第四、七、十个字符指示该文件是否可以被拥有者、和拥有者处于同一用户组的用户、其他用户运行。只需要统计相应位置的符号个数,使用grep进行正则表达式匹配,结合-c参数,即可得到符合要求的文件个数,如下:
dir=”.”;  names=$(ls -la ${dir});#echo ${names};nEXEowner=$(grep "^...x" -c <<<"${names}");nEXEgroup=$(grep "^.\{6\}x" -c <<<"${names}");nEXEother=$(grep "^.\{9\}x" -c <<<"${names}");nDirectory=$(grep "^d" -c <<<"${names}");echo "===== Counts of different files:=====";echo -e "Files executable by owner : ${nEXEowner} \n\t\t by group : ${nEXEgroup} \n\t\t by others: ${nEXEother}";echo "Directories: ${nDirectory} (including . and ..)";


0 0