红联Linux门户
Linux帮助

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

发布时间:2016-11-02 09:34:55来源:linux网站作者:archimekai
文件类型可以由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 ..)";
 
本文永久更新地址:http://www.linuxdiyf.com/linux/25648.html