Linux命令行递归的计算一个目录下一共有多少个文件

在Linux系统下,要想知道一个目录下有多少个文件,最简单的方法是,在命令行里输入
ls -l . | egrep -c '^-'
Note:
ls -1 | wc -l
Which means: ls: list files in dir
-1: (that’s a ONE) only one entry per line. Change it to -1a if you want hidden files too
|: pipe output onto…
wc: “wordcount”
-l: count lines.
这种方法只能显示当前目录下的文件数,如果想知道包括目录下里的所有子目录里的文件的数目,可以用下面的方法
方法
You seem to have the right idea. I’d use -type f to find only files:
$ find some_directory -type f | wc -l
这里还可以限制搜索的目录层级数,指定搜索的文件类型。
If you only want files directly under this directory and not to search recursively through subdirectories, you could add the -maxdepth flag:
$ find some_directory -maxdepth 1 -type f | wc -l
本文文字及图片出自 出处
