Skip to content

Commit f3e2dcf

Browse files
committed
Ch03, 05: Remove duplicated introduction of find
1 parent 40648bd commit f3e2dcf

File tree

2 files changed

+55
-43
lines changed

2 files changed

+55
-43
lines changed

docs/Ch03/index.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,8 @@ $ find [OPTION] PATH [EXPRESSION]
777777
| `-size +1M` | 大于 1M 的文件,`+` 代表大于这个大小,对应地,`-` 代表小于之后的大小 |
778778
| `-or` | 或运算符,代表它前后两个条件满足一个即可 |
779779

780+
如果不添加任何其他选项,find 会递归地输出提供的 PATH 下的所有文件。
781+
780782
!!! example "搜索示例"
781783

782784
* 在当前目录搜索名为 report.pdf 的文件:
@@ -797,6 +799,29 @@ $ find [OPTION] PATH [EXPRESSION]
797799
$ find ~/ -name 'node_modules' -type d
798800
```
799801

802+
!!! tip "对搜索到的文件批量执行命令"
803+
804+
find 的一个很有用的用法是对每一个文件都执行某个命令(例如 `md5sum`):
805+
806+
```shell
807+
find . -type f -exec md5sum {} \;
808+
```
809+
810+
这里,`find .` 是指对当前目录(`.`)进行 `find`,并只列出文件(`-type f`)。`-exec` 后面的内容是要执行的命令,其中 `{}` 会被替换成找到的对象(文件、目录)的路径,`\;` 表示对每个对象都执行一次给定的命令,即实际运行的是
811+
812+
```shell
813+
md5sum file1
814+
md5sum file2
815+
md5sum file3
816+
...
817+
```
818+
819+
如果将 `\;` 换成 `+`,那么就是将文件名称收集起来一并交给要执行的命令,即
820+
821+
```shell
822+
md5sum file1 file2 file3 ...
823+
```
824+
800825
### 模式匹配 {#pattern}
801826

802827
许多现代的 shell 都支持一定程度的模式匹配。举个例子,bash 的匹配模式被称为 [glob](https://mywiki.wooledge.org/glob),支持的操作如下:

docs/Ch05/index.md

Lines changed: 30 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -457,48 +457,35 @@ graph LR
457457

458458
## 列出文件系统项目 {#list-objects}
459459

460-
经常我们需要在 Shell 中列出某个目录下的项目(子目录和文件)。`ls` 命令是最常见的用来列出文件系统项目的命令,`ls -la` 则可以显示隐藏文件(`-a`)和更详细的信息(`-l`)。但是,`ls` 只能显示某个目录下的文件和子目录,并不会深入子目录内部继续检查。下面介绍几个命令,常用于获取这些信息。
461-
462-
### `find` 命令 {#cmd-find}
463-
464-
`find` 命令可以列出某个目录下所有的目录和文件,并**递归地**进入子目录。基本用法是
465-
466-
```shell
467-
$ find /etc
468-
/etc
469-
/etc/analog.cfg
470-
/etc/hosts.deny
471-
/etc/initramfs-tools
472-
/etc/initramfs-tools/initramfs.conf
473-
/etc/initramfs-tools/hooks
474-
/etc/initramfs-tools/conf.d
475-
/etc/initramfs-tools/conf.d/resume
476-
/etc/initramfs-tools/modules
477-
/etc/initramfs-tools/update-initramfs.conf
478-
... (省略)
479-
```
480-
481-
可以看到,`find` 命令将列出指定的目录下的文件和子目录名称,在遇到子目录时立即进入目录并递归地执行上面的操作。
482-
483-
该命令的一个很有用的用法是对每一个文件都执行某个命令(例如 `md5sum`):
484-
485-
```shell
486-
find . -type f -exec md5sum {} \;
487-
```
488-
489-
这里,`find .` 是指对当前目录(`.`)进行 `find`,并只列出文件(`-type f`)。`-exec` 后面的内容是要执行的命令,其中 `{}` 会被替换成找到的对象(文件、目录)的路径,`\;` 表示对每个对象都执行一次给定的命令,即实际运行的是
490-
491-
```shell
492-
md5sum file1
493-
md5sum file2
494-
md5sum file3
495-
...
496-
```
497-
498-
如果将 `\;` 换成 `+`,那么就是将文件名称收集起来一并交给要执行的命令,即
499-
500-
```shell
501-
md5sum file1 file2 file3 ...
460+
经常我们需要在 Shell 中列出某个目录下的项目(子目录和文件)。`ls` 命令是最常见的用来列出文件系统项目的命令,`ls -la` 则可以显示隐藏文件(`-a`)和更详细的信息(`-l`)。但是,除非添加 `-R` 参数,`ls` 只能显示某个目录下的文件和子目录,并不会深入子目录内部继续检查。下面介绍几个命令,常用于获取这些信息。
461+
462+
### `tree` 命令 {#cmd-tree}
463+
464+
`tree` 命令可以以树状图的形式显示目录的内容。它会递归进入子目录,显示所有的文件和目录:
465+
466+
```text
467+
$ tree /etc/
468+
/etc/
469+
├── NetworkManager
470+
│   ├── NetworkManager.conf
471+
│   ├── conf.d
472+
│   ├── dispatcher.d
473+
│   │   ├── no-wait.d
474+
│   │   ├── pre-down.d
475+
│   │   └── pre-up.d
476+
│   ├── dnsmasq-shared.d
477+
│   ├── dnsmasq.d
478+
│   └── system-connections [error opening dir]
479+
├── UPower
480+
│   └── UPower.conf
481+
├── X11
482+
│   ├── tigervnc
483+
│   │   └── Xsession
484+
│   └── xinit
485+
│   ├── xinitrc
486+
│   ├── xinitrc.d
487+
│   │   ├── 40-libcanberra-gtk-module.sh
488+
(以下省略)
502489
```
503490

504491
### `du` 命令 {#cmd-du}
@@ -511,7 +498,7 @@ $ du -h /etc/
511498
8.0K /etc/initramfs-tools/conf.d
512499
4.0K /etc/initramfs-tools/scripts/local-premount
513500
4.0K /etc/initramfs-tools/scripts/nfs-premount
514-
... (省略)
501+
(中间内容省略)
515502
4.0K /etc/initramfs-tools/scripts/panic
516503
4.0K /etc/initramfs-tools/scripts/local-top
517504
44K /etc/initramfs-tools/scripts

0 commit comments

Comments
 (0)