Linux:查找目录和子目录中的文件数

Sometimes we want to find the number of files in a directory in Linux. For example, finding how many images are present in JournalDev WordPress uploads directory.

有时我们想在Linux目录中查找文件数。 例如,查找JournalDev WordPress上载目录中存在多少图像。

There are various ways to do that, let’s look into some of the common scenarios and the best command to find the number of files in a directory.

有多种方法可以执行此操作,让我们研究一些常见方案和最佳命令以查找目录中的文件数。

1.查找目录中的文件数 (1. Find the Number of Files in a Directory)

We can use the ls command along with the wc command to count the number of files in a directory. Let’s count the number of files in my theme root directory.

我们可以将ls命令wc 命令一起使用来计算目录中的文件数。 让我们计算一下主题根目录中的文件数。


# ls
404.php    archive.php   functions.php  inc        layouts            phpcs.xml.dist     screenshot.png    single.php      webpack
LICENSE    comments.php  header.php     index.php  package-lock.json  postcss.config.js  search.php        src             woocommerce.css
Plugins    dist          home.php       js         package.json       readme.txt         sidebar-left.php  style.css
README.md  footer.php    images         languages  page.php           rtl.css            sidebar.php       template-parts
# ls -1 | wc -l
34
Linux Find Number Of Files In a Directory
Linux Find Number Of Files in a Directory
Linux查找目录中的文件数

If you look at the image, the blue coloured items are directories. They are also included as file in the output.

如果您查看图像,则蓝色项目是目录。 它们也作为文件包含在输出中。

What if we want to count only files and not directories?

如果我们只想计算文件而不是目录怎么办?


# ls -p | grep -v / | wc -l
24
#
  1. The “ls -p” command prints directory names with “/” at the end of it.

    “ ls -p”命令在目录名的末尾显示“ /”。
  2. The “grep -v /” command filters the output and prints only the name that doesn’t contain “/”, hence leaving out all the directories.

    “ grep -v /”命令过滤输出,仅输出不包含“ /”的名称,因此省略所有目录。
  3. Finally, “wc -l” count the lines in the output and prints it.

    最后,“ wc -l”计算输出中的行并打印。

Similarly, if you want to find the number of directories only inside a directory, use the below command.

同样,如果只想在目录中查找目录数,请使用以下命令。


# ls -p | grep / | wc -l
10
#

Note: The above commands don’t look for the hidden files, if you want the count to include hidden files too, then use “-a” option with the ls command.

注意:上面的命令不会查找隐藏文件,如果您希望计数也包括隐藏文件,请在ls命令中使用“ -a”选项。


# ls -a1 | wc -l
41
#

This command will count “.” and “..” too, so you will have to take account of that in your shell script if needed.

该命令将计数为“。” 和“ ..”,因此如果需要,您将必须在shell脚本中考虑到这一点。

2.递归查找目录和子目录中的文件数 (2. Find Number of Files in a Directory and Subdirectories Recursively)

The above examples are good to count files and directories in a directory. But, if you want to count the number of files including subdirectories also, you will have to use the find command.

上面的示例很好地计算了目录中的文件和目录。 但是,如果您还想计算包括子目录在内的文件数,则必须使用find命令


# find . -type f
./.test_file
./functions.php
./logger/class-logger-writter.php
./logger/class-logger-export.php
./logger/assets/js/base.js
./logger/assets/css/base.css
./logger/assets/css/base.less
./logger/assets/css/base.css.map
./logger/class-logger-reader.php
./class-protector.php
./class-anti-spam-plugin.php
# find . -type f | wc -l
11
#
  1. The find command “-type f” option is used to look for regular files.

    查找命令“ -type f”选项用于查找常规文件。
  2. This command will ignore all the directories, “.”, and “..” files. But, it will include hidden files in the output.

    此命令将忽略所有目录“ ..”和“ ..”文件。 但是,它将在输出中包含隐藏文件。
  3. The “wc -l” command will count the total number of lines and print it, thus giving us the count of files.

    “ wc -l”命令将对行的总数进行计数并打印出来,从而为我们提供了文件计数。
Linux Find Number Of Files In A Directory Recursively
Linux Find Number Of Files In A Directory Recursively
Linux递归查找目录中的文件数

参考文献: (References:)

翻译自: https://www.journaldev.com/38008/linux-find-number-of-files-in-a-directory-and-subdirectories

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值