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

我发现了这个:

find DIR_NAME -type f ¦ wc -l

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

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


当前回答

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

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不将其识别为命令或类似内容的断线(δ)。这就是为什么你会得到错误消息。

你可以使用

$ tree

安装后的树包用

$ sudo apt-get install tree

(在Debian / Mint / Ubuntu Linux机器上)。

该命令不仅显示文件的计数,还分别显示目录的计数。选项-L可用于指定最大显示级别(默认情况下,它是目录树的最大深度)。

通过提供-a选项,也可以包含隐藏文件。

这将完全正常工作。简单的短。如果要计算文件夹中存在的文件数量。

ls | wc -l

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

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

如果你想避免错误的情况,不要让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