如何递归地计数Linux目录中的文件?
我发现了这个:
find DIR_NAME -type f ¦ wc -l
但是当我运行它时,它返回以下错误。
查找:路径必须在表达式之前:
如何递归地计数Linux目录中的文件?
我发现了这个:
find DIR_NAME -type f ¦ wc -l
但是当我运行它时,它返回以下错误。
查找:路径必须在表达式之前:
当前回答
如果你需要递归计算特定的文件类型,你可以这样做:
find YOUR_PATH -name '*.html' -type f | wc -l
-l只是显示输出中的行数。
如果需要排除某些文件夹,请使用-not -path
find . -not -path './node_modules/*' -name '*.js' -type f | wc -l
其他回答
可以使用ncdu命令。它将递归地计算Linux目录包含多少文件。下面是一个输出示例:
它有一个进度条,如果你有很多文件,这很方便:
在Ubuntu上安装:
sudo apt-get install -y ncdu
基准:我使用https://archive.org/details/cv_corpus_v1.tar(380390个文件,11 GB)作为文件夹,其中必须计算文件的数量。
找到。-type f | wc -l:大约1m20s完成 Ncdu:大约1m20s完成
我编写了ffcnt来加快特定情况下的递归文件计数:支持区段映射的旋转磁盘和文件系统。
它可能比ls或基于find的方法快一个数量级,但是YMMV。
根据上面给出的回复和评论,我列出了下面的文件计数清单。特别是它结合了@Greg Bell提供的解决方案和@Arch Stanton的评论 & @Schneems
计数当前目录和子目录中的所有文件
function countit { find . -maxdepth 1000000 -type d -print0 | while IFS= read -r -d '' i ; do file_count=$(find "$i" -type f | wc -l) ; echo "$file_count: $i" ; done }; countit | sort -n -r >file-count.txt
计数当前目录及子目录中所有给定名称的文件
function countit { find . -maxdepth 1000000 -type d -print0 | while IFS= read -r -d '' i ; do file_count=$(find "$i" -type f | grep <enter_filename_here> | wc -l) ; echo "$file_count: $i" ; done }; countit | sort -n -r >file-with-name-count.txt
ls -l | grep -e -x -e -dr | wc -l
长串 过滤文件和dirs 计算过滤后的行号
tree $DIR_PATH | tail -1
样例输出:
5309个目录,2122个文件