如何列出OSX终端中每个目录中有多少个文件?(How can I list how many files are in each directory in OSX terminal?)

如何列出每个目录中的子文件一起有多少文件?

我找到:

find . -name "*.txt" | wc -l

但这一切都算得上。 我需要分别为每个子目录计数

预期的样本结果是:

home/folder1 12 home/folder2 10 home/ 22 etc/folder1 100 etc/folder2 200 etc/folder3 10 etc 310 ...

有没有一种方法可以计算每个目录中的文件数量?

How can I list how many files are in each directory together sub-directoies?

i found that:

find . -name "*.txt" | wc -l

but this is count all. I need the count for each sub-directory separately

Expected sample result is:

home/folder1 12 home/folder2 10 home/ 22 etc/folder1 100 etc/folder2 200 etc/folder3 10 etc 310 ...

Is there a way I can count the number of files in each directory like this?

最满意答案

你必须遍历你的目录,并在每个目录中找到.txt文件

find . -type d -exec sh -c 'echo -n "{} " && find {} -name "*.txt" | wc -l' \;

You have to loop over your directories and find .txt files inside each of them

find . -type d -exec sh -c 'echo -n "{} " && find {} -name "*.txt" | wc -l' \;如何列出OSX终端中每个目录中有多少个文件?(How can I list how many files are in each directory in OSX terminal?)

如何列出每个目录中的子文件一起有多少文件?

我找到:

find . -name "*.txt" | wc -l

但这一切都算得上。 我需要分别为每个子目录计数

预期的样本结果是:

home/folder1 12 home/folder2 10 home/ 22 etc/folder1 100 etc/folder2 200 etc/folder3 10 etc 310 ...

有没有一种方法可以计算每个目录中的文件数量?

How can I list how many files are in each directory together sub-directoies?

i found that:

find . -name "*.txt" | wc -l

but this is count all. I need the count for each sub-directory separately

Expected sample result is:

home/folder1 12 home/folder2 10 home/ 22 etc/folder1 100 etc/folder2 200 etc/folder3 10 etc 310 ...

Is there a way I can count the number of files in each directory like this?

最满意答案

你必须遍历你的目录,并在每个目录中找到.txt文件

find . -type d -exec sh -c 'echo -n "{} " && find {} -name "*.txt" | wc -l' \;

You have to loop over your directories and find .txt files inside each of them

find . -type d -exec sh -c 'echo -n "{} " && find {} -name "*.txt" | wc -l' \;