如何递归地计数Linux目录中的文件?

我发现了这个:

find DIR_NAME -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完成

其他回答

假设你想要一个每个目录的文件总数,试试:

for d in `find YOUR_SUBDIR_HERE -type d`; do 
   printf "$d - files > "
   find $d -type f | wc -l
done

对于当前目录,尝试这样做:

for d in `find . -type d`; do printf "$d - files > "; find $d -type f | wc -l; done;

如果你有很长的空格名,你需要改变IFS,像这样:

OIFS=$IFS; IFS=$'\n'
for d in `find . -type d`; do printf "$d - files > "; find $d -type f | wc -l; done
IFS=$OIFS

对于当前目录:

find -type f | wc -l

根据上面给出的回复和评论,我列出了下面的文件计数清单。特别是它结合了@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

对于名称中有空格的目录…(基于上面的各种答案)—递归打印目录名称和文件数量:

find . -mindepth 1 -type d -print0 | while IFS= read -r -d '' i ; do echo -n $i": " ; ls -p "$i" | grep -v / | wc -l ; done

示例(为便于阅读而格式化):

pwd
  /mnt/Vancouver/Programming/scripts/claws/corpus

ls -l
  total 8
  drwxr-xr-x 2 victoria victoria 4096 Mar 28 15:02 'Catabolism - Autophagy; Phagosomes; Mitophagy'
  drwxr-xr-x 3 victoria victoria 4096 Mar 29 16:04 'Catabolism - Lysosomes'

ls 'Catabolism - Autophagy; Phagosomes; Mitophagy'/ | wc -l
  138

## 2 dir (one with 28 files; other with 1 file):
ls 'Catabolism - Lysosomes'/ | wc -l
  29

使用tree可以更好地可视化目录结构:

tree -L 3 -F .
  .
  ├── Catabolism - Autophagy; Phagosomes; Mitophagy/
  │   ├── 1
  │   ├── 10
  │   ├── [ ... SNIP! (138 files, total) ... ]
  │   ├── 98
  │   └── 99
  └── Catabolism - Lysosomes/
      ├── 1
      ├── 10
      ├── [ ... SNIP! (28 files, total) ... ]
      ├── 8
      ├── 9
      └── aaa/
          └── bbb

  3 directories, 167 files

man find | grep mindep
  -mindepth levels
    Do not apply any tests or actions at levels less than levels
    (a non-negative integer).  -mindepth 1 means process all files
    except the starting-points.

Ls -p | grep -v /(下面使用)来自https://unix.stackexchange.com/questions/48492/list-only-regular-files-but-not-directories-in-current-directory的答案2

find . -mindepth 1 -type d -print0 | while IFS= read -r -d '' i ; do echo -n $i": " ; ls -p "$i" | grep -v / | wc -l ; done
./Catabolism - Autophagy; Phagosomes; Mitophagy: 138
./Catabolism - Lysosomes: 28
./Catabolism - Lysosomes/aaa: 1

应用程序:我想找到几百个目录中的最大文件数量(所有深度= 1)[下面的输出再次格式化的可读性]:

date; pwd
    Fri Mar 29 20:08:08 PDT 2019
    /home/victoria/Mail/2_RESEARCH - NEWS

time find . -mindepth 1 -type d -print0 | while IFS= read -r -d '' i ; do echo -n $i": " ; ls -p "$i" | grep -v / | wc -l ; done > ../../aaa
    0:00.03

[victoria@victoria 2_RESEARCH - NEWS]$ head -n5 ../../aaa
    ./RNA - Exosomes: 26
    ./Cellular Signaling - Receptors: 213
    ./Catabolism - Autophagy; Phagosomes; Mitophagy: 138
    ./Stress - Physiological, Cellular - General: 261
    ./Ancient DNA; Ancient Protein: 34

[victoria@victoria 2_RESEARCH - NEWS]$ sed -r 's/(^.*): ([0-9]{1,8}$)/\2: \1/g' ../../aaa | sort -V | (head; echo ''; tail)

    0: ./Genomics - Gene Drive
    1: ./Causality; Causal Relationships
    1: ./Cloning
    1: ./GenMAPP 2
    1: ./Pathway Interaction Database
    1: ./Wasps
    2: ./Cellular Signaling - Ras-MAPK Pathway
    2: ./Cell Death - Ferroptosis
    2: ./Diet - Apples
    2: ./Environment - Waste Management

    988: ./Genomics - PPM (Personalized & Precision Medicine)
    1113: ./Microbes - Pathogens, Parasites
    1418: ./Health - Female
    1420: ./Immunity, Inflammation - General
    1522: ./Science, Research - Miscellaneous
    1797: ./Genomics
    1910: ./Neuroscience, Neurobiology
    2740: ./Genomics - Functional
    3943: ./Cancer
    4375: ./Health - Disease 

sort -V是一个自然排序. ...所以,我在这些(claw Mail)目录中的最大文件数量是4375个文件。如果我在每个目录中留下(https://stackoverflow.com/a/55409116/1904943)这些文件名——它们都是以数字命名的,从1开始——并填充到总共5个数字,应该没问题。


齿顶高

查找文件总数,子目录在一个目录。

$ date; pwd
Tue 14 May 2019 04:08:31 PM PDT
/home/victoria/Mail/2_RESEARCH - NEWS

$ ls | head; echo; ls | tail
Acoustics
Ageing
Ageing - Calorie (Dietary) Restriction
Ageing - Senescence
Agriculture, Aquaculture, Fisheries
Ancient DNA; Ancient Protein
Anthropology, Archaeology
Ants
Archaeology
ARO-Relevant Literature, News

Transcriptome - CAGE
Transcriptome - FISSEQ
Transcriptome - RNA-seq
Translational Science, Medicine
Transposons
USACEHR-Relevant Literature
Vaccines
Vision, Eyes, Sight
Wasps
Women in Science, Medicine

$ find . -type f | wc -l
70214    ## files

$ find . -type d | wc -l
417      ## subdirectories

这应该可以工作:

find DIR_NAME -type f | wc -l

解释:

-type f只包括文件。 |(而不是……)将find命令的标准输出重定向到wc命令的标准输入。 Wc (word count的缩写)对输入(docs)的换行、单词和字节进行计数。 -l只计算换行。

注:

将DIR_NAME替换为。执行当前文件夹中的命令。 您还可以删除类型f以在计数中包括目录(和符号链接)。 如果文件名可以包含换行符,这个命令可能会被高估。

解释为什么你的例子不起作用:

在您所显示的命令中,您没有使用“Pipe”(|)来连接两个命令,而是使用了shell不将其识别为命令或类似内容的断线(δ)。这就是为什么你会得到错误消息。