Merge pull request #4986 from geekpi/master

translated
This commit is contained in:
geekpi 2017-01-19 12:59:11 +08:00 committed by GitHub
commit beb1db5e91
2 changed files with 75 additions and 77 deletions

View File

@ -1,77 +0,0 @@
translating---geekpi
How to Find Number of Files in a Directory and Subdirectories
============================================================
In this guide, we will cover how to display the total number of files in the current working directory or any other directory and its subdirectories on a Linux system.
We will use the [find command][6] which is used to search for files in a directory hierarchy together with [wc command][7] which prints newline, word, and byte counts for each file, alternatively data read from standard input.
Following are the options that we can use with [find command][8] as follows:
1. `-type`  specifies the file type to search for, in the case above, the `f` means find all regular files.
2. `-print`  an action to print the absolute path of a file.
3. `-l`  this option prints the total number of newlines, which is equals to the total number of absolute file paths output by [find command][1].
The general syntax of find command.
```
# find . -type f -print | wc -l
$ sudo find . -type f -print | wc -l
```
Important: Use [sudo command][9] to read all files in the specified directory including those in the subdirectories with superuser privileges, in order to avoid “Permission denied” errors as in the screen shot below:
[
![Find Number of Files in Linux](http://www.tecmint.com/wp-content/uploads/2017/01/Find-Number-of-Files-in-Linux.png)
][10]
Find Number of Files in Linux
You can see that in the first command above, not all files in the current working directory are read by findcommand.
The following are extra examples to show total number of regular files in `/var/log` and `/etc` directories respectively:
```
$ sudo find /var/log/ -type f -print | wc -l
$ sudo find /etc/ -type f -print | wc -l
```
For more examples on Linux find command and wc command go through the following series of articles for additional usage options, tips and related commands:
1. [35 Useful find Command Examples in Linux][2]
2. [How to Find Recent or Todays Modified Files in Linux][3]
3. [Find Top 10 Directoires and Files Disk Space in Linux][4]
4. [6 Useful wc Command Examples to Count Lines, Words and Characters][5]
Thats all! In case you know of any other method to display the total number of files in a directory and its subdirectories, do share it with us in the comments.
--------------------------------------------------------------------------------
作者简介:
![](http://1.gravatar.com/avatar/4e444ab611c7b8c7bcb76e58d2e82ae0?s=128&d=blank&r=g)
Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.
--------------------------------------------------------------------------------
via: http://www.tecmint.com/find-number-of-files-in-directory-subdirectories-linux/
作者:[Aaron Kili][a]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:http://www.tecmint.com/author/aaronkili/
[1]:http://www.tecmint.com/find-top-large-directories-and-files-sizes-in-linux/
[2]:http://www.tecmint.com/35-practical-examples-of-linux-find-command/
[3]:http://www.tecmint.com/find-recent-modified-files-in-linux/
[4]:http://www.tecmint.com/find-top-large-directories-and-files-sizes-in-linux/
[5]:http://www.tecmint.com/wc-command-examples/
[6]:http://www.tecmint.com/35-practical-examples-of-linux-find-command/
[7]:http://www.tecmint.com/wc-command-examples/
[8]:http://www.tecmint.com/find-recent-modified-files-in-linux/
[9]:http://www.tecmint.com/sudoers-configurations-for-setting-sudo-in-linux/
[10]:http://www.tecmint.com/wp-content/uploads/2017/01/Find-Number-of-Files-in-Linux.png

View File

@ -0,0 +1,75 @@
如何知道目录和子目录下文件的数量
============================================================
在本指南中,我们将介绍如何在 Linux 系统上显示当前工作目录或任何其他目录及其子目录中的文件数量。
我们将使用[ find 命令][6],它用于搜索目录层次结构中的文件以及[ wc 命令][7],它会打印每个文件或标准输入的换行符、单词和字节计数。
以下是我们可以使用[ find 命令][8]的选项,如下所示:
1. `-type` - 指定要搜索的文件类型,在上面的情况下,`f`表示查找所有常规文件。
2. `-print` - 打印文件绝对路径。
3. `-l` - 此选项打印换行符的总数,等于由[ find 命令][1]输出的绝对文件路径总数。
find 命令的一般语法。
```
# find . -type f -print | wc -l
$ sudo find . -type f -print | wc -l
```
重要:使用[ sudo 命令][9]读取指定目录中的所有文件,包括具有超级用户权限的子目录中的文件,以避免 “Permission denied” 错误,如下截图所示:
[
![Find Number of Files in Linux](http://www.tecmint.com/wp-content/uploads/2017/01/Find-Number-of-Files-in-Linux.png)
][10]
Linux 中的文件数量
你可以看到在上面的第一个命令中find 命令没有读取当前工作目录中的所有文件。
下面是额外的示例,分别显示 `/var/log``/etc` 目录中的常规文件总数:
```
$ sudo find /var/log/ -type f -print | wc -l
$ sudo find /etc/ -type f -print | wc -l
```
有关Linux find 和 wc 命令的更多示例,请查看以下系列文章以了解其他使用选项,提示和相关命令:
1. [35 个 Linux 中的 “find” 命令示例][2]
2. [如何在 Linux 中查找最近或今天的修改的文件][3]
3. [在 Linux 中查找十个占用最大的目录和文件][4]
4. [6 个有用的 “wc” 命令示例来计算行数、单词和字符][5]
就是这样了!如果你知道其他任何方法来显示目录及其子目录中的文件总数,请在评论中与我们分享。
--------------------------------------------------------------------------------
作者简介:
![](http://1.gravatar.com/avatar/4e444ab611c7b8c7bcb76e58d2e82ae0?s=128&d=blank&r=g)
Aaron Kili 是 Linux 和 F.O.S.S 爱好者,将来的 Linux SysAdmin、web 开发人员,目前是 TecMint 的内容创建者,他喜欢用电脑工作,并坚信分享知识。
--------------------------------------------------------------------------------
via: http://www.tecmint.com/find-number-of-files-in-directory-subdirectories-linux/
作者:[Aaron Kili][a]
译者:[geekpi](https://github.com/geekpi)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:http://www.tecmint.com/author/aaronkili/
[1]:http://www.tecmint.com/find-top-large-directories-and-files-sizes-in-linux/
[2]:http://www.tecmint.com/35-practical-examples-of-linux-find-command/
[3]:http://www.tecmint.com/find-recent-modified-files-in-linux/
[4]:http://www.tecmint.com/find-top-large-directories-and-files-sizes-in-linux/
[5]:http://www.tecmint.com/wc-command-examples/
[6]:http://www.tecmint.com/35-practical-examples-of-linux-find-command/
[7]:http://www.tecmint.com/wc-command-examples/
[8]:http://www.tecmint.com/find-recent-modified-files-in-linux/
[9]:http://www.tecmint.com/sudoers-configurations-for-setting-sudo-in-linux/
[10]:http://www.tecmint.com/wp-content/uploads/2017/01/Find-Number-of-Files-in-Linux.png