Sun Blog

Back

Linux统计文件夹下的文件数目Blur image

在Linux中,有几种方法可以统计文件夹下的文件数目:

使用ls命令结合wc命令#

统计当前目录下的文件数(不包括子目录中的文件):

ls -l | grep ^- | wc -l
bash
  • ls -l 列出详细信息
  • grep ^- 过滤出以”-“开头的行(即普通文件)
  • wc -l 计算行数

使用find命令#

统计指定目录及其子目录中的所有文件数:

find /path/to/directory -type f | wc -l
bash

只统计指定目录(不包括子目录)中的文件数:

find /path/to/directory -maxdepth 1 -type f | wc -l
bash

按文件类型统计#

统计指定目录中特定类型的文件数(例如.txt文件):

find /path/to/directory -name "*.txt" | wc -l
bash
Linux统计文件夹下的文件数目
https://blog.csun.site/blog/2025-04-21-linux-count-files-in-directory
Author Sun Xin
Published at April 21, 2025
Comment seems to stuck. Try to refresh?✨