Linux中shell脚本怎样实现遍历某个目录,并实现对该目录下文件与目录的分类统计
#!/bin/bash
#脚本名称 dir
#定义一个函数fun_directory
fun_directory() {
let "filenum=0"
let "dirnum=0"
for i in $( ls )
do
if [ -d $i ]
then
let dirnum+=1
else
let filenum+=1
fi
done
echo "The number of directorys is $dirnum"
echo "The number of files is $filenum"
}
#调用函数
fun_directory
[root@localhost csv]# ./dir.sh
path counts:14
file counts:8
[root@localhost csv]#
[root@localhost csv]# cat dir.sh
#!/bin/bash
dir=./Z
i=0
j=0
for file in find $dir
do
if [ -f $file ]
then
((i=$i+1))
else
((j=$j+1))
fi
done
echo "path counts:$j"
echo "file counts:$i"
exit 0
[root@localhost csv]#
[root@localhost csv]#
➜ ~ find Downloads -type f | wc -l
19
➜ ~ find Downloads -type d | wc -l
2
看到楼主已经采纳了别人的,我就知道 楼主想着单层级目录统计文件夹与文件数。采纳的就那样。但如果楼主想要带导级递归地统计,显然采纳的东西就过于简单了。哈