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

我发现了这个:

find DIR_NAME -type f ¦ wc -l

但是当我运行它时,它返回以下错误。

查找:路径必须在表达式之前:


当前回答

在我的电脑上,rsync比在接受的答案中找到| wc -l快一点:

$ rsync --stats --dry-run -ax /path/to/dir /tmp

Number of files: 173076
Number of files transferred: 150481
Total file size: 8414946241 bytes
Total transferred file size: 8414932602 bytes

第二行是文件的数量,在上面的例子中是150,481。作为奖励,您还可以获得总大小(以字节为单位)。

备注:

第一行是文件、目录、符号链接等的总和,这就是为什么它比第二行大。 ——dry-run(或简称-n)选项对于不实际传输文件非常重要! 我使用-x选项来“不跨越文件系统边界”,这意味着如果你为/执行它,并且附加了外部硬盘,它只会计算根分区上的文件。

其他回答

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

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 -type f | wc -l

如果您想知道当前工作目录中存在多少文件和子目录,可以使用这一行程序

find . -maxdepth 1 -type d -print0 | xargs -0 -I {} sh -c 'echo -e $(find {} | wc -l) {}' | sort -n

这将在GNU风格下工作,对于BSD linux(例如OSX),只需在echo命令中省略-e即可。

如果你需要递归计算特定的文件类型,你可以这样做:

find YOUR_PATH -name '*.html' -type f | wc -l 

-l只是显示输出中的行数。

如果需要排除某些文件夹,请使用-not -path

find . -not -path './node_modules/*' -name '*.js' -type f | wc -l

如果你想避免错误的情况,不要让wc -l看到有换行符的文件(它将被算作2+文件)

例:考虑这样一种情况,我们有一个文件,其中只有一个EOL字符

> mkdir emptydir && cd emptydir
> touch $'file with EOL(\n) character in it'
> find -type f
./file with EOL(?) character in it
> find -type f | wc -l
2

因为至少gnu wc似乎没有一个选项来读取/计数一个以空结束的列表(除了从文件中),最简单的解决方案就是不传递文件名,而是每次找到文件时输出一个静态输出,例如在上面的同一个目录中

> find -type f -exec printf '\n' \; | wc -l
1

或者你的发现是否支持

> find -type f -printf '\n' | wc -l
1