Shell脚本统计当前目录下目录和文件的数量
Linux下如何统计当前目录下文件有多少个,目录又有多少个呢?
下面用shell写一个脚本,放置在当前目录下,执行即可。
 
#!/bin/bash
 
#脚本名称dir
#定义一个函数fun_directory
 
fun_directory(){     
 let"filenum=0"
 let"dirnum=0"
 
foriin$(ls)
do
  if[-d$i]
  then
     letdirnum+=1
  else
     letfilenum+=1
  fi
done
 
echo"Thenumberofdirectorysis$dirnum"
echo"Thenumberoffilesis$filenum"
 
}
 
#调用函数
 
fun_directory
我们测试一下:
[root@localhostscripts]#pwd /root/scripts [root@localhostscripts]#ll|sort drwxr-xr-x2rootroot409606-1210:44charpter8 drwxr-xr-x2rootroot409606-1312:34aaa -rw-r--r--1rootroot 10506-1308:56file1 -rw-r--r--1rootroot 10606-1214:248-9 -rw-r--r--1rootroot 12106-1209:36jiu -rw-r--r--1rootroot 13306-1311:09temp -rw-r--r--1rootroot 21006-1213:408-8 -rw-r--r--1rootroot 22206-1211:518-6 -rw-r--r--1rootroot 24706-1211:358-5 -rw-r--r--1rootroot 27306-1213:138-7 -rw-r--r--1rootroot 29206-1210:578-1 -rw-r--r--1rootroot 30906-1214:518-11 -rw-r--r--1rootroot 31406-1215:018-17 -rw-r--r--1rootroot 31706-1312:10test -rw-r--r--1rootroot 5106-1211:008-2 -rw-r--r--1rootroot 5306-1308:51file -rw-r--r--1rootroot 6706-1310:1710-4 -rw-r--r--1rootroot 7806-1310:09test.out -rwxr-xr-x1rootroot 10306-1211:068-3 -rwxr-xr-x1rootroot 12406-1310:0210-32 -rwxr-xr-x1rootroot 30406-1312:47dir -rwxr-xr-x1rootroot 31606-1211:218-4 #执行脚本 [root@localhostscripts]#shdir Thenumberofdirectorysis2 Thenumberoffilesis20 [root@localhostscripts]#
可以看到,数据统计是准确的。
