Merge pull request #7206 from Flowsnow/master

翻译完成-How To Count The Number Of Files And Folders/Directories In Linux
This commit is contained in:
Xingyu.Wang 2018-01-15 22:48:06 +08:00 committed by GitHub
commit 39a3f7557c
2 changed files with 163 additions and 181 deletions

View File

@ -1,181 +0,0 @@
translating by Flowsnow
How To Count The Number Of Files And Folders/Directories In Linux
======
Hi folks, today again we came with set of tricky commands that help you in many ways. It's kind of manipulation commands which help you to count files and directories in the current directory, recursive count, list of files created by particular user, etc,.
In this tutorial, we are going to show you, how to use more than one command like, all together to perform some advanced actions using ls, egrep, wc and find command. The below set of commands which helps you in many ways.
To experiment this, i'm going to create totally 7 files and 2 folders (5 regular files & 2 hidden files). See the below tree command output which clearly shows the files and folder lists.
**Suggested Read :** [File Manipulation Commands][1]
```
# tree -a /opt
/opt
├── magi
│   └── 2g
│   ├── test5.txt
│   └── .test6.txt
├── test1.txt
├── test2.txt
├── test3.txt
├── .test4.txt
└── test.txt
2 directories, 7 files
```
**Example-1 :** To count current directory files (excluded hidden files). Run the following command to determine how many files there are in the current directory and it doesn't count dotfiles.
```
# ls -l . | egrep -c '^-'
4
```
**Details :**
* `ls` : list directory contents
* `-l` : Use a long listing format
* `.` : List information about the FILEs (the current directory by default).
* `|` : control operator that send the output of one program to another program for further processing.
* `egrep` : print lines matching a pattern
* `-c` : General Output Control
* `'^-'` : This respectively match the empty string at the beginning and end of a line.
**Example-2 :** To count current directory files which includes hidden files. This will include dotfiles as well in the current directory.
```
# ls -la . | egrep -c '^-'
5
```
**Example-3 :** Run the following command to count current directory files & folders. It will count all together at once.
```
# ls -1 | wc -l
5
```
**Details :**
* `ls` : list directory contents
* `-l` : Use a long listing format
* `|` : control operator that send the output of one program to another program for further processing.
* `wc` : It's a command to print newline, word, and byte counts for each file
* `-l` : print the newline counts
**Example-4 :** To count current directory files & folders which includes hidden files & directory.
```
# ls -1a | wc -l
8
```
**Example-5 :** To count current directory files recursively which includes hidden files.
```
# find . -type f | wc -l
7
```
**Details :**
* `find` : search for files in a directory hierarchy
* `-type` : File is of type
* `f` : regular file
* `wc` : It's a command to print newline, word, and byte counts for each file
* `-l` : print the newline counts
**Example-6 :** To print directories & files count using tree command (excluded hidden files).
```
# tree | tail -1
2 directories, 5 files
```
**Example-7 :** To print directories & files count using tree command which includes hidden files.
```
# tree -a | tail -1
2 directories, 7 files
```
**Example-8 :** Run the below command to count directory recursively which includes hidden directory.
```
# find . -type d | wc -l
3
```
**Example-9 :** To count the number of files based on file extension. Here we are going to count `.txt` files.
```
# find . -name "*.txt" | wc -l
7
```
**Example-10 :** Count all files in the current directory by using the echo command in combination with the wc command. `4` indicates the amount of files in the current directory.
```
# echo * | wc
1 4 39
```
**Example-11 :** Count all directories in the current directory by using the echo command in combination with the wc command. `1` indicates the amount of directories in the current directory.
```
# echo comic/ published/ sources/ translated/ | wc
1 1 6
```
**Example-12 :** Count all files and directories in the current directory by using the echo command in combination with the wc command. `5` indicates the amount of directories and files in the current directory.
```
# echo * | wc
1 5 44
```
**Example-13 :** To count number of files in the system (Entire system)
```
# find / -type f | wc -l
69769
```
**Example-14 :** To count number of folders in the system (Entire system)
```
# find / -type d | wc -l
8819
```
**Example-15 :** Run the following command to count number of files, folders, hardlinks, and symlinks in the system (Entire system)
```
# find / -type d -exec echo dirs \; -o -type l -exec echo symlinks \; -o -type f -links +1 -exec echo hardlinks \; -o -type f -exec echo files \; | sort | uniq -c
8779 dirs
69343 files
20 hardlinks
11646 symlinks
```
--------------------------------------------------------------------------------
via: https://www.2daygeek.com/how-to-count-the-number-of-files-and-folders-directories-in-linux/
作者:[Magesh Maruthamuthu][a]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:https://www.2daygeek.com/author/magesh/
[1]:https://www.2daygeek.com/empty-a-file-delete-contents-lines-from-a-file-remove-matching-string-from-a-file-remove-empty-blank-lines-from-a-file/

View File

@ -0,0 +1,163 @@
如何统计Linux中文件和文件夹/目录的数量
======
嗨,伙计们,今天我们又来了一系列棘手的命令,会多方面帮助你。 这是一种操作命令,它可以帮助您计算当前目录中的文件和目录,递归计数,特定用户创建的文件列表等。
在本教程中我们将向您展示如何使用多个命令并使用lsegrepwc和find命令执行一些高级操作。 下面的命令很有帮助。
为了实验我打算总共创建7个文件和2个文件夹5个常规文件和2个隐藏文件。 看到下面的tree命令的输出清楚的展示文件和文件夹列表。
**推荐阅读** [文件操作命令][1]
```
# tree -a /opt
/opt
├── magi
│   └── 2g
│   ├── test5.txt
│   └── .test6.txt
├── test1.txt
├── test2.txt
├── test3.txt
├── .test4.txt
└── test.txt
2 directories, 7 files
```
**示例-1 ** 统计当前目录文件(排除隐藏文件)。 运行以下命令以确定当前目录中有多少个文件并且不计算点文件LCTT译者注点文件即当前目录文件和上级目录文件
```
# ls -l . | egrep -c '^-'
4
```
**细节:**
* `ls` 列出目录内容
* `-l` 使用长列表格式
* `.` 列出有关文件的信息(默认为当前目录)
* `|` 控制操作器将一个程序的输出发送到另一个程序进行进一步处理
* `egrep` 打印符合模式的行
* `-c` 通用输出控制
* `'^-'` 它们分别匹配一行的开头和结尾的空字符串
**示例-2 ** 统计包含隐藏文件的当前目录文件。 包括当前目录中的点文件。
```
# ls -la . | egrep -c '^-'
5
```
**示例-3 ** 运行以下命令来计算当前目录文件和文件夹。 它会一次计算所有的。
```
# ls -1 | wc -l
5
```
**细节:**
* `ls` 列出目录内容
* `-l` 使用长列表格式
* `|` 控制操作器将一个程序的输出发送到另一个程序进行进一步处理
* `wc` 这是一个为每个文件打印换行符,字和字节数的命令
* `-l` 打印换行符数
**示例-4 ** 统计包含隐藏文件和目录的当前目录文件和文件夹。
```
# ls -1a | wc -l
8
```
**示例-5 ** 递归计算当前目录文件,其中包括隐藏文件。
```
# find . -type f | wc -l
7
```
**细节 **
* `find` 搜索目录层次结构中的文件
* `-type` 文件类型
* `f` 常规文件
* `wc` 这是一个为每个文件打印换行符,字和字节数的命令
* `-l` 打印换行符数
**示例-6 ** 使用tree命令打印目录和文件数排除隐藏文件
```
# tree | tail -1
2 directories, 5 files
```
**示例-7 ** 使用包含隐藏文件的树命令打印目录和文件数。
```
# tree -a | tail -1
2 directories, 7 files
```
**示例-8 ** 运行下面的命令递归计算包含隐藏目录的目录。
```
# find . -type d | wc -l
3
```
**示例-9 ** 根据文件扩展名计算文件数量。 这里我们要计算 `.txt` 文件。
```
# find . -name "*.txt" | wc -l
7
```
**示例-10 ** 使用echo命令和wc命令统计当前目录中的所有文件。 `4`表示当前目录中的文件数量。
```
# echo * | wc
1 4 39
```
**示例-11 ** 通过使用echo命令和wc命令来统计当前目录中的所有目录。 `1`表示当前目录中的目录数量。
```
# echo comic/ published/ sources/ translated/ | wc
1 1 6
```
**示例-12 ** 通过使用echo命令和wc命令来统计当前目录中的所有文件和目录。 `5`表示当前目录中的目录和文件的数量。
```
# echo * | wc
1 5 44
```
**示例-13 ** 统计系统(整个系统)中的文件数。
```
# find / -type f | wc -l
69769
```
**示例-14 ** 统计系统(整个系统)中的文件夹数。
```
# find / -type d | wc -l
8819
```
**示例-15 ** 运行以下命令来计算系统(整个系统)中的文件,文件夹,硬链接和符号链接数。
```
# find / -type d -exec echo dirs \; -o -type l -exec echo symlinks \; -o -type f -links +1 -exec echo hardlinks \; -o -type f -exec echo files \; | sort | uniq -c
8779 dirs
69343 files
20 hardlinks
11646 symlinks
```
--------------------------------------------------------------------------------
via: https://www.2daygeek.com/how-to-count-the-number-of-files-and-folders-directories-in-linux/
作者:[Magesh Maruthamuthu][a]
译者:[Flowsnow](https://github.com/Flowsnow)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:https://www.2daygeek.com/author/magesh/
[1]:https://www.2daygeek.com/empty-a-file-delete-contents-lines-from-a-file-remove-matching-string-from-a-file-remove-empty-blank-lines-from-a-file/