欢迎来到罐子的实验室
本次目标
授人以渔——不让你死记命令,而是教你遇到任何命令都能自己查出怎么用。
学完你能做什么:
- 用
man 阅读命令手册,看懂 SYNOPSIS 格式
1. 为什么要学查文档
Linux 有几千个命令,每个命令又有一堆选项。没有人能全记住,也没必要记。高手和菜鸟的区别往往不是「记得多」,而是「查得快」。
当你学会查文档,遇到新命令就能自己摸索,不用每次都去搜百度、问 AI。这一模块就是教你「自带说明书」的打开方式。
2. man:命令的官方手册
man(手册)是最权威的文档来源。给它一个命令名,它就显示那个命令的手册页:
会进入一个全屏的阅读界面,大致长这样:
LS(1) User Commands LS(1)NAME ls - list directory contentsSYNOPSIS ls [OPTION]... [FILE]...DESCRIPTION List information about the FILEs (the current directory by default). Sort entries alphabetically... -a, --all do not ignore entries starting with . -l use a long listing format ...
操作方式(这是关键,很多人卡在这里)
man 用的是 less 这个分页器,操作跟 vim 类似:
💡 新手最常见的困惑:进了 man 不知道怎么出来——按 q 就能退出。
动手练习 1:读懂 ls 手册
任务:打开 ls 的手册,找到 -h 选项是干什么用的。
思路引导:man ls → 按 / 输入 -h 回车搜索 → 找到说明。
参考答案:在 DESCRIPTION 区找到 -h, --human-readable —— 配合 -l 用 K/M/G 显示文件大小。所以 ls -lh 输出里的 4.0K 就是它的功劳。
常见错误:
- ❌ 进
man 后按 Ctrl-C 试图退出——没用,要按 q。
3. 读懂 SYNOPSIS
手册顶部的 SYNOPSIS(概要)用一套固定符号告诉你命令怎么调用,看懂它就读懂了「语法」:
符号约定:
举例,ls [OPTION]... [FILE]... 表示:
所以这些都是合法的:ls、ls -l、ls -la /etc /tmp、ls --color=auto。
动手练习 2:解析 SYNOPSIS
任务:看 cp 的 SYNOPSIS,判断「源文件」「目标」哪个必填哪个可选。
思路引导:man cp,看顶部的 SYNOPSIS 行。
参考答案:
cp [OPTION]... [-T] SOURCE DESTcp [OPTION]... SOURCE... DIRECTORY
可以看到:SOURCE 没有方括号(必填),DEST/DIRECTORY 也没方括号(必填),OPTION 有方括号(可选)。所以 cp 至少要给「源」和「目标」两个参数。
4. 在手册里搜索
手册页可能很长(man bash 有几千行)。用 / 搜索关键词能快速定位。
比如想找 find 怎么按文件大小搜索:
user@host:~$ man find/find -size# 在 man 界面里输入,回车
按 n 跳到下一个匹配,N 跳上一个。
动手练习 3:搜索 find 的用法
任务:在 find 手册里找出「按修改时间」搜索的选项是什么。
思路引导:man find → /mtime → 看 -mtime 的说明。
参考答案:-mtime n 表示「文件最后一次修改是在 n 天前」。比如 find . -mtime -7 找最近 7 天内修改过的文件。
5. --help:快速参考
man 太长时,很多程序提供 --help 或 -h 参数,打印一份精简版用法说明,直接显示在终端里(不进分页器):
user@host:~$ ls --help用法:ls [选项]... [文件]...List information about the FILEs (the current directory by default).Sort entries alphabetically...Mandatory arguments to long options are mandatory for short options too. -a, --all do not ignore entries starting with . -A, --almost-all do not list implied . and .. ...
--help 适合:
- 管道处理,比如
ls --help | grep size 找含 "size" 的行(→ 详见《模块 06》)
man 适合:
💡 选项名约定:长选项用两个短横(--all),短选项用一个短横 + 单字母(-a)。它们通常对应同一个功能,-a 是 --all 的简写。
动手练习 4:对比 man 与 --help
任务:分别用 man mkdir 和 mkdir --help 看 mkdir 的用法,体会两者的差别。
参考答案:
user@host:~$ mkdir --help用法:mkdir [选项]... 目录...Create the DIRECTORY(ies), if they do not already exist.Mandatory arguments to long options are mandatory for short options too. -m, --mode=MODE set file mode -p, --parents no error if existing, make parent directories as needed -v, --verbose print a message for each created directory ...
--help 一屏看完,man 更详细。
常见错误:不是所有程序都支持 --help——个别程序把 -h 当成别的含义(比如 head -h 就不是帮助)。两种都试一下。
6. help:内建命令专用
有些「命令」其实不是独立程序,而是 Shell 内建的(built-in),比如 cd、echo、read、export。它们没有独立的 man 手册(或者说 man 里查到的是另一个东西),要用 Shell 自己的 help 命令:
user@host:~$ help cdcd: cd [-L|[-P [-e]] [-@]] [dir] Change the shell working directory. ...
列出所有内建命令:
怎么知道一个命令是内建还是外部程序?用 type:
user@host:~$ type cdcd is a shell builtin# 内建user@host:~$ type lsls is aliased to `ls --color=auto'# 别名user@host:~$ type catcat is /bin/cat# 外部程序
动手练习 5:分清内建与外部
任务:判断 echo、pwd、grep 哪些是内建,哪些是外部程序,并分别查它们的文档。
思路引导:先 type 判断,内建用 help,外部用 man。
参考答案:
user@host:~$ type echo pwd grepecho is a shell builtinpwd is a shell builtingrep is /usr/bin/grepuser@host:~$ help echo# 内建,用 helpecho: echo [-neE] [arg ...] Write arguments to the standard output.user@host:~$ man grep# 外部,用 man
💡 注意:echo 既是个内建,/bin/echo 也存在。你输入 echo 时 Shell 优先用内建版。
7. 查不到怎么办
如果 man、--help、help 都查不到,说明:
- 程序没装——
command not found,先装(如 apt install)。 - 是个脚本——用
cat 看脚本内容(→ 详见《模块 11》)。
动手练习 6:排查一个「假命令」
任务:很多系统上 ls 是个别名。用 type ls 和 \ls(反斜杠绕过别名)对比看看。
参考答案:
user@host:~$ type lsls is aliased to `ls --color=auto'user@host:~$ \ls# 绕过别名,调用原始 ls,没有彩色
速查表
| | |
|---|
man 命令 | | |
命令 --help | | |
help 命令 | | |
type 命令 | | |
info 命令 | | |
man 分页器操作
SYNOPSIS 符号
下一步
查文档很方便,但手动输入长路径、长文件名还是很累。Shell 有个神器能帮你「少打字」——通配符和 Tab 补全。