Linux 下统计文件夹中文件的数量

虽然,很小白。但是,还是那句话,好记性不如烂笔头,卸下来吧。命令还是得常用才行。

方法一:

这里涉及几个命令:

命令1: ls -l === ll 其实不用太多解释,这个是入门命令,当然,还有-R参数。列出子目录和文件

命令2: grep “^-” grep我的理解是GNU正则(GNU regular expression)的缩写,这里是匹配以’-'开头的信息。因为,ll列出来的包括目录是以’d'开头的标示

命令3: wc -l wc命令(word characters)统计文件字符数,参数 -l 是统计行数。因此,通过管道符操作就可以将ll的文件列表的行数转化为文件数


统计当前文件夹下的文件数量,和 包括子文件下的数量

[root@localhost ~]# ll | grep "^-" | wc -l
22
[root@localhost ~]# ll -R | grep "^-" | wc -l
34

方法二:

这里涉及如下两个命令:

命令1: find ./ -type f man中对于’f'参数的解释如下:

-type c
              File is of type c:
              b      block (buffered) special
              c      character (unbuffered) special
              d      directory
              p      named pipe (FIFO)
              f      regular file
              l      symbolic link; this is never true if the -L option or the -follow option is in effect,
                     unless  the symbolic link is broken.  If you want to search for symbolic links when -L
                     is in effect, use -xtype.
              s      socket
              D      door (Solaris)

命令2: wc -l 上面解释了。这里不再陈述

[root@localhost ~]# find ./ -type f | wc -l
34
[root@localhost ~]#


需要注意的是这里命令的区别:

1. find 命令输出结果的速度要比ll的快很多。

2. find 方法是会查找子目录的。与 ll 命令的针对性不太相同。