-
Notifications
You must be signed in to change notification settings - Fork 92
Ch03, 05, 06: Update find/grep intro; Systemd fhs note #99
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f3e2dcf
Ch03, 05: Remove duplicated introduction of find
taoky 55b78db
Ch03: Add fd
taoky db61875
Ch06: Add ripgrep
taoky a6a3650
Ch03: 标点
taoky 7d74f7f
Ch03: Move du, ncdu, tree from Ch05
taoky 358af29
Ch03: Add eza and bat
taoky 7a0b81f
Ch05: Mention systemd version of fhs
taoky 6ce297a
Ch03: batcat in Debian/Ubuntu
taoky bb7e5a4
Ch03: Use fence title format
taoky e201a2e
Ch03: Make du match with other intros
taoky File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -488,6 +488,57 @@ Ubuntu 官方源位于国外,往往会有速度与延迟上的限制,可以 | |
|
|
||
| 在 Linux 在进行操作文件与目录是使用 Linux 最基础的一个技能。不像在 Windows 和 macOS 下有图形化界面,拖拽文件即可完成文件的移动,很容易管理文件与目录,Linux 的命令行操作虽然繁琐一些,但一旦上手,就可以通过命令与参数的组合完成通过图形化界面难以实现或者无法实现的功能。 | ||
|
|
||
| ### 查看文件夹内容 {#ls} | ||
|
|
||
| [第二章](../Ch02/index.md)已经介绍过 `ls` 的基本用法,这里再补充一些常用的选项。 | ||
|
|
||
| ```console | ||
| $ # -l 参数会以列表的形式输出文件的详细信息 | ||
| $ ls -l [DIRECTORY] | ||
| $ # -a 参数会显示所有文件,包括隐藏文件(以 . 开头的文件) | ||
| $ ls -a [DIRECTORY] | ||
| $ # -h 参数会以人类可读的方式显示文件大小,例如 1K、234M、2G 等 | ||
| $ ls -h [DIRECTORY] | ||
| ``` | ||
|
|
||
| !!! example "ls 示例" | ||
|
|
||
| * 以列表的形式显示当前目录下的所有文件(包括隐藏文件) | ||
|
|
||
| ```console | ||
| $ ls -la | ||
| ``` | ||
|
|
||
| * 以人类可读的方式显示当前目录下的所有文件(包括隐藏文件)的详细信息 | ||
|
|
||
| ```console | ||
| $ ls -lha | ||
| ``` | ||
|
|
||
| !!! tip "tree" | ||
|
|
||
| `tree` 命令可以以树状图的形式显示目录结构,使用前需要先安装: | ||
|
|
||
| ```console | ||
| $ sudo apt install tree | ||
| ``` | ||
|
|
||
| 使用方法: | ||
|
|
||
| ```console | ||
| $ tree [DIRECTORY] | ||
| ``` | ||
|
|
||
| !!! tip "eza" | ||
|
|
||
| [eza](https://github.com/eza-community/eza) 是一个 `ls` 的替代品,输出更加好看。可以使用如下命令安装: | ||
|
|
||
| ```console | ||
| $ sudo apt install eza | ||
| ``` | ||
|
|
||
| 其使用方法和 `ls` 基本相同,例如显示详细信息则使用 `eza -l`。 | ||
|
|
||
| ### 查看文件内容 {#view} | ||
|
|
||
| #### cat {#cat} | ||
|
|
@@ -515,6 +566,20 @@ $ cat [OPTION] FILE | |
|
|
||
| 当然和猫咪没有关系,cat 这里是 con**cat**enate(连接)的缩写,因为 cat 工具实际的功能是连接多个文件,然后输出。但是当只有一个文件的时候,cat 就会直接输出这个文件,所以 cat 最常见的用途就是输出单个文件。 | ||
|
|
||
| !!! tip "bat" | ||
|
|
||
| [bat](https://github.com/sharkdp/bat) 是一个 `cat` 的替代品,输出支持语法高亮和分页。可以使用如下命令安装: | ||
|
|
||
| ```console | ||
| $ sudo apt install bat | ||
| ``` | ||
|
|
||
| 例如,输出 sol1.c 文件的内容(支持 C 语言语法高亮): | ||
|
|
||
| ```console | ||
| $ bat sol1.c | ||
|
||
| ``` | ||
|
|
||
| #### less {#less} | ||
|
|
||
| less 和 cat 的区别在于,cat 会一次性打印全部内容到终端中并退出,而 less 一次只显示一页,且支持向前/后滚动、搜索等功能。如果要在一个大文件中(例如 man page)查找一部分内容,less 通常要比 cat 方便得多。 | ||
|
|
@@ -777,6 +842,8 @@ $ find [OPTION] PATH [EXPRESSION] | |
| | `-size +1M` | 大于 1M 的文件,`+` 代表大于这个大小,对应地,`-` 代表小于之后的大小 | | ||
| | `-or` | 或运算符,代表它前后两个条件满足一个即可 | | ||
|
|
||
| 如果不添加任何其他选项,find 会递归地输出提供的 PATH 下的所有文件。 | ||
|
|
||
| !!! example "搜索示例" | ||
|
|
||
| * 在当前目录搜索名为 report.pdf 的文件: | ||
|
|
@@ -797,6 +864,83 @@ $ find [OPTION] PATH [EXPRESSION] | |
| $ find ~/ -name 'node_modules' -type d | ||
| ``` | ||
|
|
||
| !!! tip "对搜索到的文件批量执行命令" | ||
|
|
||
| find 的一个很有用的用法是对每一个文件都执行某个命令(例如 `md5sum`): | ||
|
|
||
| ```shell | ||
| find . -type f -exec md5sum {} \; | ||
| ``` | ||
|
|
||
| 这里,`find .` 是指对当前目录(`.`)进行 `find`,并只列出文件(`-type f`)。`-exec` 后面的内容是要执行的命令,其中 `{}` 会被替换成找到的对象(文件、目录)的路径,`\;` 表示对每个对象都执行一次给定的命令,即实际运行的是: | ||
|
|
||
| ```shell | ||
| md5sum file1 | ||
| md5sum file2 | ||
| md5sum file3 | ||
| ... | ||
| ``` | ||
|
|
||
| 如果将 `\;` 换成 `+`,那么就是将文件名称收集起来一并交给要执行的命令,即: | ||
|
|
||
| ```shell | ||
| md5sum file1 file2 file3 ... | ||
| ``` | ||
|
|
||
| !!! tip "fd" | ||
|
|
||
| [fd](https://github.com/sharkdp/fd) 是 find 的更现代、好用的替代。在 Ubuntu 下需要安装 `fd-find` 包,并且命令名为 **`fdfind`**(而非 `fd`)。 | ||
|
|
||
| 其默认接受正则表达式(见[第九章](../Ch09/index.md))作为搜索条件,例如搜索结尾为 `.conf` 的文件: | ||
|
|
||
| ```console | ||
| $ fdfind '\.conf$' /etc/ | ||
| /etc/debconf.conf | ||
| /etc/gai.conf | ||
| /etc/host.conf | ||
| (以下省略) | ||
| ``` | ||
|
|
||
| ### 统计文件或文件夹大小 {#du} | ||
|
|
||
| `du` 命令可以统计文件和目录的大小。目录的大小是无法直接获取的,需要统计里面所有的文件和子目录的大小之后加和才能得到。`du` 命令的输出类似这样: | ||
|
|
||
| ```console | ||
| $ du -h /etc/ | ||
| 4.0K /etc/initramfs-tools/hooks | ||
| 8.0K /etc/initramfs-tools/conf.d | ||
| 4.0K /etc/initramfs-tools/scripts/local-premount | ||
| 4.0K /etc/initramfs-tools/scripts/nfs-premount | ||
| (中间内容省略) | ||
| 4.0K /etc/initramfs-tools/scripts/panic | ||
| 4.0K /etc/initramfs-tools/scripts/local-top | ||
| 44K /etc/initramfs-tools/scripts | ||
| 72K /etc/initramfs-tools | ||
| 12K /etc/udisks2 | ||
| 16K /etc/fonts/conf.d | ||
| 60K /etc/fonts/conf.avail | ||
| 84K /etc/fonts | ||
| ``` | ||
|
|
||
| 由于前面说到的原因,`du` 需要先递归进入子目录,处理完其中所有的项目之后,才能回到上层目录并显示上层目录的总大小。 | ||
|
|
||
| 此外,`ncdu` 命令可以以图形化和交互式的方式显示目录的内容和大小,并可以用左右方向键浏览目录,类似 Windows 的文件资源管理器。这非常便于观察哪个目录占用了较大的磁盘空间。按 `d` 可以删除当前选中的文件或目录,按 `q` 退出。 | ||
|
|
||
| ```plain | ||
| ncdu 1.18 ~ Use the arrow keys to navigate, press ? for help | ||
| --- /home/example/path ------------------------------------- | ||
| 53.1 MiB [##########] /main | ||
| 45.4 MiB [######## ] Contents-riscv64.gz | ||
| 40.6 MiB [####### ] /universe | ||
| 580.0 KiB [ ] /multiverse | ||
| 44.0 KiB [ ] /restricted | ||
| 8.0 KiB [ ] InRelease | ||
| 8.0 KiB [ ] Release | ||
| 4.0 KiB [ ] Release.gpg | ||
|
|
||
| Total disk usage: 139.7 MiB Apparent size: 139.6 MiB Items: 29 | ||
| ``` | ||
|
|
||
| ### 模式匹配 {#pattern} | ||
|
|
||
| 许多现代的 shell 都支持一定程度的模式匹配。举个例子,bash 的匹配模式被称为 [glob](https://mywiki.wooledge.org/glob),支持的操作如下: | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这层额外的缩进是否可以用 code block 的 title 代替?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
另外 VS Code 的某个插件会提示
console语言适用于同时展示命令及其输出的场景,而非只展示命令。不过这似乎是本书约定?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
对,这个规则我加到忽略列表里面了,至于要不要改可以讨论。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
确实更好看一些。这一章的我改掉吧。