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 scripts]# pwd
/root/scripts
[root@localhost scripts]# ll |sort
drwxr-xr-x 2 root root 4096 06-12 10:44 charpter8
drwxr-xr-x 2 root root 4096 06-13 12:34 aaa
-rw-r--r-- 1 root root 105 06-13 08:56 file1
-rw-r--r-- 1 root root 106 06-12 14:24 8-9
-rw-r--r-- 1 root root 121 06-12 09:36 jiu
-rw-r--r-- 1 root root 133 06-13 11:09 temp
-rw-r--r-- 1 root root 210 06-12 13:40 8-8
-rw-r--r-- 1 root root 222 06-12 11:51 8-6
-rw-r--r-- 1 root root 247 06-12 11:35 8-5
-rw-r--r-- 1 root root 273 06-12 13:13 8-7
-rw-r--r-- 1 root root 292 06-12 10:57 8-1
-rw-r--r-- 1 root root 309 06-12 14:51 8-11
-rw-r--r-- 1 root root 314 06-12 15:01 8-17
-rw-r--r-- 1 root root 317 06-13 12:10 test
-rw-r--r-- 1 root root 51 06-12 11:00 8-2
-rw-r--r-- 1 root root 53 06-13 08:51 file
-rw-r--r-- 1 root root 67 06-13 10:17 10-4
-rw-r--r-- 1 root root 78 06-13 10:09 test.out
-rwxr-xr-x 1 root root 103 06-12 11:06 8-3
-rwxr-xr-x 1 root root 124 06-13 10:02 10-32
-rwxr-xr-x 1 root root 304 06-13 12:47 dir
-rwxr-xr-x 1 root root 316 06-12 11:21 8-4
#执行脚本
[root@localhost scripts]# sh dir
The number of directorys is 2
The number of files is 20
[root@localhost scripts]#
可以看到,数据统计是准确的。
更新日志
- 潘美辰.2006-最冷的夏天(引进版)【美梦辰真】【WAV+CUE】
- 班得瑞原装进口《第十张新世纪专辑:月光水岸》1CD[APE/CUE分轨][277.2MB]
- 班得瑞原装进口《第十一张新世纪专辑:雾色山脉》1CD[APE/CUE分轨][291.6MB]
- 班得瑞原装进口《第十二张新世纪专辑:翡翠谷》1CD[APE/CUE分轨][307.9MB]
- 高卿尘.2024-情绪商店【Hahahai】【FLAC分轨】
- 王恩喆.2024-黄沙【KOOD】【FLAC分轨】
- 邓丽君.1983-漫步人生路(2024环球MQA-UHQCD限量版)【环球】【WAV+CUE】
- 陈容森.1996-情断【上华】【WAV+CUE】
- 裘海正.1994-爱我的人和我爱的人【上华】【WAV+CUE】
- 庾澄庆.1988-错过的爱【福茂】【WAV+CUE】
- 班得瑞原装进口《第十三张新世纪专辑:旭日之丘》1CD[APE/CUE分轨][329.6MB]
- 纯音入心系列纯音乐《古筝|佛蕴|境法禅心意法自然》1CD[MP3][350MB]
- 纪钧瀚《钢琴阅读时光 雨中书店聆听轻音乐》[320K/MP3][203.44MB]
- 群星.1981-名曲65(2014环球复黑王·百代篇)【EMI百代】【WAV+CUE】
- 陈淑桦.1990-娃娃的故事【柯达】【WAV+CUE】