Linux系统下统计文件夹、文件数量的命令
本文以以centos 7.3 为例:
[root@centos73_test ~ 16:42:41]# cat /etc/redhat-release
CentOS Linux release 7.3.1611 (Core)
[root@centos73_test ~ 16:42:41]# uname -a
Linux yum-server 3.10.0-514.el7.x86_64 #1 SMP Tue Nov 22 16:42:41 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
环境:
-
创建文件夹
[root@centos73_test ~ 17:28:11]#mkdir /filedir [root@centos73_test ~ 17:31:23]#cd /filedir/ [root@centos73_test /filedir 17:31:29]#mkdir -p {a..c}{1..3} [root@centos73_test /filedir 17:32:08]#ls a1 a2 a3 b1 b2 b3 c1 c2 c3 [root@centos73_test /filedir 17:32:11]#cd a1 [root@centos73_test /filedir/a1 17:41:30]#mkdir -p {d..e}{4..5} [root@centos73_test /filedir/a1 17:43:02]#mkdir -p ../b2/{f..g}{6..7} [root@centos73_test /filedir/a1 17:48:11]#cd e4 [root@centos73_test /filedir/a1/e4 17:58:45]#ls update //上传文件夹 [root@centos73_test /filedir/a1/e4 18:01:06]#pwd /filedir/a1/e4
-
查看当前目录下的文件数量(不包含子目录中的文件)
[root@centos73_test /filedir/a1/e4 18:01:08]#cd /filedir/ [root@centos73_test /filedir 18:02:49]#ll total 36 drwxr-xr-x 6 root root 4096 Sep 23 17:42 a1 drwxr-xr-x 2 root root 4096 Sep 23 17:32 a2 drwxr-xr-x 2 root root 4096 Sep 23 17:32 a3 drwxr-xr-x 2 root root 4096 Sep 23 17:32 b1 drwxr-xr-x 6 root root 4096 Sep 23 17:43 b2 drwxr-xr-x 2 root root 4096 Sep 23 17:32 b3 drwxr-xr-x 2 root root 4096 Sep 23 17:32 c1 drwxr-xr-x 2 root root 4096 Sep 23 17:32 c2 drwxr-xr-x 2 root root 4096 Sep 23 17:32 c3 [root@centos73_test /filedir 18:02:55]#ls -l|grep "^-"| wc -l 0
-
查看当前目录下的文件数量(包含子目录中的文件) 注意:R,代表子目录
[root@centos73_test /filedir 18:02:59]#ls -lR|grep "^-"| wc -l 784
-
查看当前目录下的文件夹目录个数(不包含子目录中的目录),同上述理,如果需要查看子目录的,加上R
[root@centos73_test /filedir 18:04:25]#ls -l|grep "^d"| wc -l 9
[root@centos73_test /filedir 18:04:51]#ls -lR|grep "^d"| wc -l 18
-
查询当前路径下的指定前缀名的目录下的所有文件数量
例如:统计所有以“a”开头的目录下的全部文件数量
[root@centos73_test /filedir 18:05:19]#ls -lR a*/|grep "^-"| wc -l 784
对每个命令参数做一下说明备注:
ls -l
该命令表示以长列表输出指定目录下的信息(未指定则表示当前目录),R代表子目录中的“文件”,这个“文件”指的是目录、链接、设备文件等的总称
“^d” 表示目录,"^-" 表示文件
wc -l
表示统计输出信息的行数,因为经过前面的过滤已经只剩下普通文件,一个目录或文件对应一行,所以统计的信息的行数也就是目录或文件的个数