Linux中获取帮助、vim 编辑器、输入输出重定向
Linux 中获取帮助
Man 手册
man 简介
man,manual 简称。man 使用不同 section 区分手册类别。
| 1 | 用户命令(可执行命令和 shell 程序) |
| 2 | 系统调用(从用户空间调用的内核例程) |
| 3 | 库函数(由程序库提供) |
| 4 | 特殊文件(如设备文件) |
| 5 | 文件格式(用于许多配置文件和结构) |
| 6 | 游戏(过去的有趣程序章节) |
| 7 | 惯例、标准和其他(协议、文件系统) |
| 8 | 系统管理和特权命令(维护任务) |
| 9 | Linux 内核 API(内核调用) |
man 命令
[chenxin@centos7 ~ 17:01:18]$ man –help
Usage: man [OPTION...] [SECTION] PAGE...
man 命令将会调用 less 打开 /usr/share/man/ 相关文件
passwd(1) 描述 passwd 命令更改密码:
[chenxin@centos7 ~ 17:01:45]$ man 1 passwd
passwd(5) 解释 /etc/passwd 文件格式:
[chenxin@centos7 ~ 17:02:10]$ man 5 passwd
查看英文的帮助手册,指定 LANG 变量:
[chenxin@centos7 ~ 17:02:32]$ LANG=en_US.utf8 man gedit
或者:
[chenxin@centos7 ~ 17:02:51]$ LANG=en_US.utf8
[chenxin@centos7 ~ 17:03:05]$ man gedit
man 手册页面交互操作指令(less 程序)
| PageDown 或者空格键 | 向前(向下)滚动一个屏幕 |
| PageUp | 向后(向上)滚动一个屏幕 |
| 向下箭头 | 向前(向下)滚动一行 |
| 向上箭头 | 向后(向上)滚动一行 |
| d | 向前(向下)滚动半个屏幕 |
| u | 向后(向上)滚动半个屏幕 |
| /string | 在 man page 中向前(向下)搜索 string |
| n | 在 man page 中重复之前的向前(向下)搜索 |
| N | 在 man page 中重复之前的向后(向上)搜索 |
| g | 转到 man page 的第一行 |
| G | 转到 man page 的最后一行 |
| q | 退出 man,并返回到 shell 命令提示符 |
使用 root 用户更新 man page 库:
[root@centos7 ~ 17:03:40]# mandb
正在删除 /usr/share/man 里的旧数据库条目...
mandb: 警告:/usr/share/man/man8/fsck.fat.8.manpage-fix.gz:忽略错误的文件名
... ...
正在处理 /usr/share/man/en 下的手册页...
正在删除 /usr/local/share/man 里的旧数据库条目...
正在处理 /usr/local/share/man 下的手册页...
0 man subdirectories contained newer manual pages.
0 manual pages were added.
0 stray cats were added.
17 old database entries were purged.
使用 -k 选项搜索关键字:
[chenxin@centos7 ~ 17:04:57]$ man -k passwd
chgpasswd (8) – 批量更新组密码
chpasswd (8) – 批量更新密码
gpasswd (1) – administer /etc/group and /etc/gshadow
mkpasswd (1) – 为用户产生新口令
passwd (5) – (未知的主题)
... ...
[chenxin@centos7 ~ 17:05:24]$ man -k ext4
.....
resize2fs (8) – ext2/ext3/ext4 file system resizer
......
[chenxin@centos7 ~ 17:05:49]$ man resize2fs
补充:命令的来源
查看 shell 自带命令
不建议使用 man 查询:
[chenxin@centos7 ~ 17:06:00]$ man history
使用 help command:
[chenxin@centos7 ~ 17:06:20]$ help history
history: history [-c] [-d 偏移量] [n] 或 history -anrw [文件名] 或 history -ps 参数 [参数...]
显示或操纵历史列表。
带行号显示历史列表,将每个被修改的条目加上前缀 `*'。
参数 N 会仅列出最后的 N 个条目。
... ...
重要: 通用选项 –help,也用于查看命令帮助。
/usr/share/doc
可以通过 file:///usr/share/doc 浏览。一些包还含拓展示例,配置文件模板,脚本,使用说明或用户指导手册。例如:/usr/share/doc/dhclient-*。
[chenxin@centos7 ~ 17:06:50]$ cat /usr/share/doc/dhclient-4.2.5/dhclient.conf.example
which 和 whereis 命令
- which查找命令的可执行文件路径
#查看 vi 命令代表什么
[chenxin@centos7 ~ 17:07:20]$ which vi
alias vi='vim'
/usr/bin/vim
- whereis 查看关键字对应的程序位置,帮助文档,源码:
[chenxin@centos7 ~ 17:07:35]$ whereis man
man: /usr/bin/man /usr/share/man /usr/share/man/man1/man.1.gz /usr/share/man/man1p/man.1p.gz /usr/share/man/man7/man.7.gz
课后练习
查找并查看以下 man 手册,并举例验证
查看 man 命令使用方法。
[chenxin@centos7 ~ 10:40:05]$man –help
查看 gedit 命令使用方法:如何使用gedit打开文件、打开文件的时候如何定位到特定行和列。
[chenxin@centos7 ~ 10:40:46]$man gedit
#进入之后查看里面的内容
GEDIT(1) General Commands Manual GEDIT(1)
NAME
gedit – text editor for the GNOME Desktop
SYNOPSIS
gedit [OPTION...] [FILE...] [+LINE[:COLUMN]]
gedit [OPTION...] –
DESCRIPTION
gedit is the official text editor of the GNOME desktop environment.
While aiming at simplicity and ease of use, gedit is a powerful general purpose text editor. It
can be used to create and edit all kinds of text files.
gedit features a flexible plugin system which can be used to dynamically add new advanced fea-
tures to gedit itself.
[chenxin@centos7 ~ 10:42:17]$ gedit /etc/passwd
[chenxin@centos7 ~ 10:43:23]$ edit /etc/passwd +行:列
[chenxin@centos7 ~ 11:04:49]$ gedit /etc/passwd +3:2 —–3行2列
查看 su 命令使用方法:不带参数作用和带 – 作用。
[chenxin@centos7 ~ 10:46:07]$man su
#进入之后搜索without,查看不带参数
-, -l, –login
Starts the shell as login shell with an environment similar to a real login:
o clears all environment variables except for TERM
o initializes the environment variables HOME, SHELL, USER, LOGNAME, PATH
o changes to the target user's home directory
o sets argv[0] of the shell to '-' in order to make the shell a login shell
[chenxin@centos7 ~ 10:46:07]$ su
[chenxin@centos7 ~ 10:46:07]$ su -l root
密码:
上一次登录:四 7月 16 09:53:17 CST 2026pts/0 上
最后一次失败的登录:四 7月 16 10:46:06 CST 2026pts/3 上
最有一次成功登录后有 1 次失败的登录尝试。
查找 调整内核(kernel)启动(boot)参数 手册。
#查找kernel手册,管道过滤boot,找到了第一个是题目要求的
[chenxin@centos7 ~ 10:56:25]$ man -k kernel | grep boot
bootparam (7) – introduction to boot time parameters of the Linux kernel
kernel-install (8) – Add and remove kernel and initramfs images to and from /boot
kexec (8) – directly boot into a new kernel
modules-load.d (5) – Configure kernel modules to load at boot
sysctl.d (5) – Configure kernel parameters at boot
systemd-modules-load (8) – Load kernel modules at boot
systemd-modules-load.service (8) – Load kernel modules at boot
systemd-sysctl (8) – Configure kernel parameters at boot
systemd-sysctl.service (8) – Configure kernel parameters at boot
#查找这个找到的手册
[chenxin@centos7 ~ 10:57:25]$ man bootparam
#进入手册
BOOTPARAM(7) Linux Programmer's Manual BOOTPARAM(7)
NAME
bootparam – introduction to boot time parameters of the Linux kernel
DESCRIPTION
The Linux kernel accepts certain 'command-line options' or 'boot time parameters' at the moment
it is started. In general this is used to supply the kernel with information about hardware
parameters that the kernel would not be able to determine on its own, or to avoid/override the
values that the kernel would otherwise detect.
When the kernel is booted directly by the BIOS (say from a floppy to which you copied a kernel
using 'cp zImage /dev/fd0'), you have no opportunity to specify any parameters. So, in order to
take advantage of this possibility you have to use a boot loader that is able to pass parameters,
such as GRUB.
The argument list
The kernel command line is parsed into a list of strings (boot arguments) separated by spaces.
Most of the boot arguments take have the form:
name[=value_1][,value_2]…[,value_10]
where 'name' is a unique keyword that is used to identify what part of the kernel the associated
values (if any) are to be given to. Note the limit of 10 is real, as the present code handles
only 10 comma separated parameters per keyword. (However, you can reuse the same keyword with up
to an additional 10 parameters in unusually complicated situations, assuming the setup function
supports it.)
Manual page bootparam(7) line 1 (press h for help or q to quit)
查找 创建 archive 工具 手册,并使用对应工具创建archive文件和提取archive文件。(不允许使用 zip和unzip)。
#查找archive手册
[chenxin@centos7 ~ 10:50:09]$ man -k archive
ar (1) – create, modify, and extract from archives
ar (1p) – create and maintain library archives
cpio (1) – copy files to and from archives
cpio (5) – format of cpio archive files
cpio.h (0p) – cpio archive values
db_archive (1) – Find unused log files for archival
funzip (1) – filter for extracting from a ZIP archive in a pipe
gpg-zip (1) – Encrypt or sign files into an archive
osinfo-db-export (1) – Export to a osinfo database archive
osinfo-db-import (1) – Import an osinfo database archive
pax (1p) – portable archive interchange
ranlib (1) – generate index to archive.
rpm2cpio (8) – Extract cpio archive from RPM Package Manager (RPM) package.
tar (5) – format of tape archive files
unzip (1) – list, test and extract compressed files in a ZIP archive
unzipsfx (1) – self-extracting stub for prepending to ZIP archives
zip (1) – package and compress (archive) files
zipgrep (1) – search files in a ZIP archive for lines matching a pattern
zipinfo (1) – list detailed information about a ZIP archive
#查找cpio手册,
#/搜索create是创建
cpio {-o|–create}
#/搜索extract是提取
cpio {-i|–extract}
[chenxin@centos7 ~ 11:06:49]$ man cpio
CPIO(1) General Commands Manual CPIO(1)
NAME
cpio – 存取归档包中的文件
总览 (SYNOPSIS)
cpio {-o|–create} [-0acvABLV] [-C bytes] [-H format] [-M message] [-O [[user@]host:]archive] [-F
[[user@]host:]archive] [–file=[[user@]host:]archive] [–format=format] [–message=message]
[–null] [–reset-access-time] [–verbose] [–dot] [–append] [–block-size=blocks] [–derefer‐
ence] [–io-size=bytes] [–quiet] [–force-local] [–help] [–version] < name-list [> archive]
cpio {-i|–extract} [-bcdfmnrtsuvBSV] [-C bytes] [-E file] [-H format] [-M message] [-R
[user][:.][group]] [-I [[user@]host:]archive] [-F [[user@]host:]archive]
[–file=[[user@]host:]archive] [–make-directories] [–nonmatching] [–preserve-modification-
time] [–numeric-uid-gid] [–rename] [–list] [–swap-bytes] [–swap] [–dot] [–unconditional]
[–verbose] [–block-size=blocks] [–swap-halfwords] [–io-size=bytes] [–pattern-file=file]
[–format=format] [–owner=[user][:.][group]] [–no-preserve-owner] [–message=message]
[–force-local] [–no-absolute-filenames] [–sparse] [–only-verify-crc] [–quiet] [–help]
[–version] [pattern...] [< archive]
cpio {-p|–pass-through} [-0adlmuvLV] [-R [user][:.][group]] [–null] [–reset-access-time]
[–make-directories] [–link] [–quiet] [–preserve-modification-time] [–unconditional] [–ver‐
bose] [–dot] [–dereference] [–owner=[user][:.][group]] [–no-preserve-owner] [–sparse]
[–help] [–version] destination-directory < name-list
vim 编辑器
vim 编辑器介绍
每一个系统管理员都有一个偏爱的文本编辑器。有的管理员偏爱 gedit,有的偏爱 nano,甚至有的偏爱 emacs。即使已经有偏爱的编辑器,了解 vim 还是有必要的。因为 vim 可以安装在任何系统。
vim 版本,Linux 系统中可以安装 vim 三个不同版本:
- vim-minimal:只提供 vi 和相关命令。
- vim-enhanced:提供 vim 命令,提供语法高亮,拼写检查等特性。
- vim-X11:提供 gvim,图形界面下的 vim。gvim 包含菜单栏。当用户不知道命令的时候,可以通过菜单操作。
安装 vim 工具:
[root@centos7 ~ 17:08:20]# yum install vim
vim 模式
[chenxin@centos7 ~ 17:09:50]$ cp /etc/profile ./profile.sh
[chenxin@centos7 ~ 17:10:05]$ vim ./profile.sh
命令行模式快捷键

定位文件内容:
- w,向后 1 个 word(光标在单词开头);e,向后 1 个 word(光标在单词末尾)
- b,向前 1 个 word
- ^ 或 0,行首
- $,行尾
- (,上一个句子头部;),下一个句子尾部
- {,上一个自然段;},下一个自然段
- gg,第一行;G,最后一行
less 使用 g,第一行;G,最后一行。
进入插入模式:
- i,当前位置插入。(insert)
- I,行首插入。
- a,当前位置追加。(append)
- A,行末追加。
- o,向下新开一个空白行。
- O,向上新开一个空白行。
复制:
- yy,复制 1 行,相当于 y1y。以此类推 y2y y2w y2b。
粘贴:
- p 当前位置后粘贴,P 当前位置前粘贴
删除:
- x,删除当前单个字符。5x,删除当前字符和后续 4 个字符。
- X,删除前一单个字符。
- dw,删除 1 个 word,以此类推 d5w d3b d2d d$。
- D,从当前删除到最后,相当于 d$。
回退:
- u,撤销上一个操作。
- U,撤销整行修改。
- Ctrl+r,反向撤销,与 u 相反。
修改,相当于删除后插入:
- s,删除当前字符,并进入编辑模式。
- S,删除当前行,并进入编辑模式。
- c,代表替换动作,例如 cw,删除 1 个 word,并进入编辑模式。以此类推 c5w。
- C,从当前删除到最后,并进入编辑模式。
替换:
- r,替换单个字符后回退到命令行模式。
- R,替换模式,一直替换直到按 Esc 返回命令行模式。
练习示例
示例 1
1.使用 vim ./profile.sh 打开文件。
2.根据任务要求,多次重复此文本输入循环:
- 使用方向键定位光标。
- 按 i 键进入插入模式。
- 输入文本 hello world。
- 按 Esc 键返回命令模式。
- 若有需要,使用 u 撤销当前行中出错的编辑操作。
- 若有需要,按 ctrl+r 键反向撤销当前行中出错的编辑操作。
3.根据任务要求,多次重复此文本删除循环:
- 使用方向键定位光标。
- 按 x 键删除选定的文本。
- 若有需要,使用 u 撤销当前行中出错的编辑操作。
4.若要保存或退出,可选择下列操作之一来写入或放弃文本编辑:
- 按 :w 写入(保存)文件,并留在命令模式中进行更多编辑。
- 按 :wq 写入文件并退出 Vim。
- 按 :q! 退出 Vim,但放弃上次写入以来进行的所有更改。
示例 2
1.使用 vim ./profile.sh 打开文件。
2.根据任务要求,多次重复此文本选择循环:
- 使用方向键将光标定位到第一个字符。
- 按 v 键进入可视模式(选择对象)。
- 使用方向键将光标定位到最后一个字符。
- 按 y 键拖拉(复制)所选内容。
- 使用方向键将光标定位到插入位置。
- 按 p 键放置(粘贴)所选内容。
3.若要保存或退出,可选择下列操作之一来写入或放弃文本编辑:
- 按 :w 写入(保存)文件,并留在命令模式中进行更多编辑。
- 按 :wq 写入文件并退出 Vim。
- 按 :q! 退出 Vim,但放弃上次写入以来进行的所有更改。
示例 3
完成 DHCP 服务配置文件定制
配置仓库:
[root@centos7 ~ 17:15:30]# curl -s -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
[root@centos7 ~ 17:15:45]# curl -s -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
安装 dhcp 软件:
[root@centos7 ~ 17:16:00]# yum install -y dhcp
准备配置文件:
[chenxin@centos7 ~ 17:17:30]$ cp /usr/share/doc/dhcp*/dhcpd.conf.example ~/dhcpd.conf
编辑 ~/dhcpd.conf 文件:
| :set number showcmd | 显示行号和操作命令 |
| gg | 光标定位到第一行 |
| d46d | 删除当前行以及后面的 45 行 |
| :11,28d | 删除 11 到 28 行 |
| /You | 搜索字符串 You,并定位到该字符位置 |
| dG | 删除当前位置行到最后 |
| :%s/10.5.5/10.1.8/g | 将所有行中 10.5.5 替换为 10.1.8 |
| /224 | 定位这个数字 |
| cw | 替换为 0,然后 esc |
| 手动操作 | 将 30 换成 130 |
| C + 输入内容 | 光标定位到 ns1,修改 dns 为 223.5.5.5:C 然后输入 223.5.5.5; |
| r | 修改路由器为 10.1.8.2,使用 r 替换末尾 1 为 2 |
| 手动修改 | 修改广播地址 10.1.8.31 为 10.1.8.255 |
| ZZ | 保存退出 |
% 针对所有内容,s 查找并替换,g 替换查找到的所有内容。
vim 配置
两个位置:
- 全局:/etc/vimrc,给所有用户设置一些默认值。
- 用户:~/.vimrc,特定用户的个性化设置。
常见的设置命令:
[chenxin@centos7 ~ 17:20:15]$ vim ~/.vimrc
set number
set showcmd
set ai ts=2
set cursorcolumn
set cursorline
参数说明:
- set number 显示行号
- set showcmd 显示操作命令
- set ai ts=2 ai 自动缩进;ts,tabspace 使用 2 个空格代替
- set cursorcolumn 列高亮对齐显示
- set cursorline 行高亮对齐显示
Linux 输入输出重定向
Linux 输入输出重定向介绍
Unix/Linux 系统中进程(PROCESS)的输入输出流(I/O Streams)模型,核心是 “标准流(Standard Streams)” 的概念,用于描述进程如何与外部(如键盘、屏幕、文件)进行数据交互。
标准流的核心组成
-
stdin(标准输入,编号 0):
- 是进程接收输入数据的通道,默认连接到 键盘,程序通过它读取用户输入(如 scanf、read 等操作会从 stdin 获取数据)。
- 用法为 仅读取(只能从该通道输入数据到进程)。
-
stdout(标准输出,编号 1):
- 是进程输出正常结果的通道,默认连接到 终端屏幕,程序的普通打印(如 printf、echo 等)会输出到 stdout。
- 用法为 仅写入(进程只能向该通道输出数据)。
-
stderr(标准错误,编号 2):
- 是进程输出错误信息的通道,默认也连接到 终端屏幕,用于区分正常输出和错误提示(如程序崩溃、逻辑错误的提示信息)。
- 用法为 仅写入(专门用于输出错误内容)。
-
3+ 号通道(其他文件,filename):
- 代表进程与 自定义文件 的交互通道(非标准流),没有默认连接,需要手动指定文件。
- 用法为 读取和/或写入(可根据需求对文件进行读、写操作)。
流程逻辑
- 输入:用户通过键盘向 stdin 输入数据,进程从 stdin 读取这些输入。
- 输出:进程将正常结果写入 stdout,错误信息写入 stderr,两者默认都显示在终端;同时,进程也可通过 3+ 号通道与其他文件进行数据的读写交互。
这种模型是 Unix/Linux 系统中 “一切皆文件” 思想的体现,让进程能以统一的方式处理不同来源/目标的数据,也为 重定向(Redirect) 等操作(如把 stdout 输出到文件、把文件内容作为 stdin 输入)提供了基础。
标准输出重定向到文件

查找 /etc 目录下文件名是 passwd 的所有文件:
[chenxin@centos7 ~ 17:25:30]$ find /etc -name 'passwd'
find: '/etc/pki/CA/private': 权限不够
find: '/etc/pki/rsyslog': 权限不够
find: '/etc/grub.d': 权限不够
/etc/pam.d/passwd
/etc/passwd
find: '/etc/dhcp': 权限不够
find: '/etc/lvm/archive': 权限不够
find: '/etc/lvm/backup': 权限不够
... ....
将找到的文件名清单 覆盖写入 到 passwd.list 文件中:
[chenxin@centos7 ~ 17:26:00]$ find /etc -name 'passwd' 1>passwd.list
find: '/etc/pki/CA/private': 权限不够
find: '/etc/pki/rsyslog': 权限不够
find: '/etc/grub.d': 权限不够
... ...
[chenxin@centos7 ~ 17:26:20]$ cat passwd.list
/etc/pam.d/passwd
/etc/passwd
补充说明:1>passwd.list 可简写为 >passwd.list;1 和 > 之间不能有空格。
将多个文件内容合并为 1 个:
[chenxin@centos7 ~ 17:26:40]$ cat /etc/fstab /etc/hosts > allinone
将找到的文件名清单 追加写入 到 passwd.list 文件中:
[chenxin@centos7 ~ 17:27:00]$ find /etc -name 'passwd' 1>>passwd.list
find: '/etc/pki/CA/private': 权限不够
find: '/etc/pki/rsyslog': 权限不够
find: '/etc/grub.d': 权限不够
......
[chenxin@centos7 ~ 17:27:20]$ cat passwd.list
/etc/pam.d/passwd
/etc/passwd
/etc/pam.d/passwd
/etc/passwd
将错误信息 覆盖写入 到 passwd.err 文件中:
[chenxin@centos7 ~ 17:27:40]$ find /etc -name 'passwd' 2>passwd.err
/etc/pam.d/passwd
/etc/passwd
[chenxin@centos7 ~ 17:28:00]$ cat passwd.err
find: '/etc/pki/CA/private': 权限不够
find: '/etc/pki/rsyslog': 权限不够
find: '/etc/grub.d': 权限不够
......
将错误信息扔掉(输出到 /dev/null,即黑洞设备):
[chenxin@centos7 ~ 17:28:20]$ find /etc -name 'passwd' 2>/dev/null
/etc/pam.d/passwd
/etc/passwd
将错误信息 追加写入 到 passwd.err 文件中:
[chenxin@centos7 ~ 17:28:40]$ find /etc -name 'passwd' 2>>passwd.err
/etc/pam.d/passwd
/etc/passwd
[chenxin@centos7 ~ 17:29:00]$ cat passwd.err
find: '/etc/pki/CA/private': 权限不够
find: '/etc/pki/rsyslog': 权限不够
find: '/etc/grub.d': 权限不够
......
将错误信息、正确信息 覆盖 写入到不同文件:
[chenxin@centos7 ~ 17:29:20]$ find /etc -name 'passwd' >passwd.list 2>passwd.err
将错误信息、正确信息 追加 写入到不同文件:
[chenxin@centos7 ~ 17:29:40]$ find /etc -name 'passwd' >>passwd.list 2>>passwd.err
用于清空文件内容,也可用于创建一个空文件 testfile:
[chenxin@centos7 ~ 17:30:00]$ > testfile
如果不存在,则创建一个文件;如果文件存在,则啥也不干:
[chenxin@centos7 ~ 17:30:15]$ >> testfile

同时将错误信息、正确信息 覆盖 写入到相同文件:
[chenxin@centos7 ~ 17:30:30]$ find /etc -name 'passwd' &> passwd.all
同时将错误信息、正确信息 追加 写入到相同文件:
[chenxin@centos7 ~ 17:30:45]$ find /etc -name 'passwd' &>> passwd.all
标准输出重定向到程序
管道符(|)实现:将上一个命令的输出作为标准输入传递给下一个命令。

示例 1:查看哪个 man page 可以 resize ext4 文件系统大小
搜索所有 ext4 相关手册,并保存到 ext4.man 文件中:
[chenxin@centos7 ~ 17:31:10]$ man -k ext4 > ext4.man
过滤 ext4.man 文件中含有 resize 的行:
[chenxin@centos7 ~ 17:31:33]$ grep resizer ext4.man
以上两个命令可合并为一行:
[chenxin@centos7 ~ 17:31:47]$ man -k ext4 | grep resizer
resize2fs (8) – ext2/ext3/ext4 file system resizer
示例 2:获取 ens33 网卡 ip 地址
[chenxin@centos7 ~ 17:32:00]$ ip addr | grep 'ens33$' | awk '{print $2}'|cut -d/ -f1
10.1.8.88
注意:管道符号后的命令必须能够接收标准输入,例如 ls 不 接收管道传递过来的标准输入。
[chenxin@centos7 ~ 17:32:30]$ echo /etc/passwd | ls -l
总用量 2
-rw-rw-r–. 1 chenxin chenxin 0 12月 15 15:08 file
-rw-rw-r–. 1 chenxin chenxin 0 12月 15 15:08 file-a
补充:|& — 标准错误和标准输出同时传递给下一个命令,作为下一个命令的标准输入
[chenxin@centos7 ~ 17:33:00]$ ls -l file-{01..06}
ls: cannot access 'file-04': No such file or directory
ls: cannot access 'file-05': No such file or directory
ls: cannot access 'file-06': No such file or directory
-rw-rw-r–. 1 chenxin chenxin 0 Dec 24 14:53 file-01
-rw-rw-r–. 1 chenxin chenxin 0 Dec 24 14:53 file-02
-rw-rw-r–. 1 chenxin chenxin 0 Dec 24 14:53 file-03
普通管道只能传 stdout,stderr 还会打到屏幕:
[chenxin@centos7 ~ 17:33:20]$ ls -l file-{01..06} |grep file-06
ls: cannot access 'file-04': No such file or directory
ls: cannot access 'file-05': No such file or directory
ls: cannot access 'file-06': No such file or directory
[chenxin@centos7 ~ 17:33:35]$ ls -l file-{01..06} |grep file-03
ls: cannot access 'file-04': No such file or directory
ls: cannot access 'file-05': No such file or directory
ls: cannot access 'file-06': No such file or directory
-rw-rw-r–. 1 chenxin chenxin 0 Dec 24 14:53 file-03
使用 |& 同时传递 stdout 和 stderr:
[chenxin@centos7 ~ 17:33:50]$ ls -l file-{01..06} |& grep file-06
ls: cannot access 'file-06': No such file or directory
tee 命令

tee 命令作用:接收管道传递过来的 stdin,并继续作为 stdout 输出。
[chenxin@centos7 ~ 17:34:20]$ > passwd.list
[chenxin@centos7 ~ 17:34:30]$ cat passwd.list
tee 命令覆盖保存到文件:
[chenxin@centos7 ~ 17:34:40]$ find /etc -name 'passwd' 2>/dev/null | tee passwd.list
/etc/pam.d/passwd
/etc/passwd
[chenxin@centos7 ~ 17:35:00]$ cat passwd.list
/etc/pam.d/passwd
/etc/passwd
tee 命令追加保存到文件(-a 选项):
[chenxin@centos7 ~ 17:35:15]$ find /etc -name 'passwd' 2>/dev/null |tee -a passwd.list
/etc/pam.d/passwd
/etc/passwd
[chenxin@centos7 ~ 17:35:35]$ cat passwd.list
/etc/pam.d/passwd
/etc/passwd
/etc/pam.d/passwd
/etc/passwd
tee 的标准输出继续通过管道传递下去:
[chenxin@centos7 ~ 17:35:50]$ man -k passwd | tee passwd.man | grep passwd
标准输入重定向给程序
常规查看文件:
[chenxin@centos7 ~ 17:36:20]$ cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
cat 命令从标准输入读取数据(通过 < 把文件作为 stdin):
[chenxin@centos7 ~ 17:36:40]$ cat < /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain
cat 直接从键盘(stdin 默认)读取:
[chenxin@centos7 ~ 17:37:00]$ cat
hello # 键盘输入hello后回车
hello # 屏幕上显示hello
ctrl+d # 输入完成,退出
shell 读取到 EOF 时,代表标准输入完成(here-document <<):
[chenxin@centos7 ~ 17:37:30]$ cat << EOF
> hello
> world
> EOF
hello
world
here-string <<<,把字符串直接作为 stdin:
[chenxin@centos7 ~ 17:38:00]$ cat <<< 'hello world'
hello world
判定一个命令是否接收标准输入:最简单的方式就是将文件内容作为标准输入重定向给程序。
思考:如何一次性将多行内容写入到文件中?
方法 1:使用引导:
[chenxin@centos7 ~ 17:38:30]$ echo 'hello world
> hello python
> hello linux' > multi_lines.txt
[chenxin@centos7 ~ 17:38:50]$ cat multi_lines.txt
hello world
hello python
hello linux
方法 2:使用 cat 重定向:
[chenxin@centos7 ~ 17:39:10]$ cat > multi_lines.txt << EOF
> hello world
> hello python
> hello linux
> EOF
[chenxin@centos7 ~ 17:39:30]$ cat multi_lines.txt
hello world
hello python
hello linux
课后练习
谈谈你对输出重定向的理解,用示例说明。
理解:Linux 中每个进程默认都有 1 号(stdout 标准输出)和 2 号(stderr 标准错误)两个"写出通道",默认两者都打印到终端屏幕。输出重定向就是把这两个通道的"出口"从屏幕拔下来,接到指定文件上,实现「结果存盘、日志归档、错误分离」。
常用符号:
- > :覆盖写 stdout(文件已有内容会被清空)
- >>:追加写 stdout(写在文件末尾,保留原内容)
- 2> :覆盖写 stderr(只收集报错)
- &> :stdout + stderr 一起合流写到同一个文件
示例 1:分别把正常结果和错误存到两个文件:
[chenxin@centos7 ~ 19:50:18]$ find /etc -name 'passwd' >passwd.list 2>passwd.err
执行后 passwd.list 里只有找到的文件路径(stdout),passwd.err 里只有"权限不够"等报错(stderr),两者分离互不干扰。
示例 2:把不需要的警告直接丢弃(写进 /dev/null 黑洞):
[chenxin@centos7 ~ 19:49:12]$ find /etc -name 'passwd' 2>/dev/null
/etc/pam.d/passwd
/etc/passwd
屏幕上只剩下干净的匹配结果,干扰信息被丢弃。
谈谈你对管道符(|)的理解,用示例说明。
理解:管道符 | 的作用是把「左边命令的 stdout」直接喂给「右边命令的 stdin」,数据在内存里流动,不需要落地写临时文件。它就像工厂流水线——左边工位加工完的半成品直接递给右边工位,是把零散命令组合成复杂逻辑的核心机制。
核心前提:右边的命令必须支持从 stdin 读数据(如 grep、awk、sed、wc、sort、head、tail、less 等)。像 ls、mkdir 这种只读命令行参数的命令,放在管道右边没有效果。
示例 1:两段管道筛选 man -k 的结果:
[chenxin@centos7 ~ 19:53:12]$ man -k ext4 | grep resizer
resize2fs (8) – ext2/ext3/ext4 file system resizer
左边 man -k ext4 吐出所有 ext4 相关命令列表,右边 grep 从中筛选出包含 resizer 的那一行,一步到位。
示例 2:四段经典流水线提取网卡 IP:
[chenxin@centos7 ~ 19:53:35]$ ip addr | grep 'ens33$' | awk '{print $2}' | cut -d/ -f1
10.1.8.88
四段分别做:① 输出全部网卡信息 → ② 筛出 ens33 那一行 → ③ 切出第 2 列(IP/掩码)→ ④ 去掉斜杠后面的掩码,留下纯 IP。
谈谈你对 tee 命令的理解,用示例说明。
理解:tee 是管道里的"三通接头"——它从 stdin 拿一份数据,同时做两件事:① 写一份到指定文件存档;② 原封不动把完整数据送到 stdout,继续给下游管道消费。解决了「长管道中间结果看不见、出了错不知道哪段有问题」的痛点。
常用参数:
- tee 文件名:覆盖式写文件
- tee -a 文件名:追加式写文件(append,多次执行日志拼在一起,日常更常用)
示例 1:一边显示到屏幕,一边存盘:
[chenxin@centos7 ~ 19:56:35]$ find /etc -name 'passwd' 2>/dev/null | tee passwd.list
/etc/pam.d/passwd
/etc/passwd
命令跑完后屏幕上看到了结果,passwd.list 文件里也同步保存了一份同样的内容。
示例 2:tee 夹在管道中间存快照,方便事后排查:
[chenxin@centos7 ~ 19:58:05]$ man -k passwd | tee passwd.man | grep passwd
执行完后:passwd.man 文件里保存了 完整的 man -k passwd 原始输出(留档),屏幕上只显示经过 grep 过滤后的结果(看重点)。将来如果怀疑过滤逻辑有误,可以直接翻 passwd.man 对比原始数据,不用重新跑。
谈谈你对标准输入重定向的理解,用示例说明。
理解:默认情况下进程的 0 号 stdin(标准输入)连着键盘,用户敲什么进程就读什么。输入重定向就是把 stdin 的"入口"从键盘切换到文件或字符串,让进程直接从文件 / 字符串里拿数据,不用等用户手工敲。
常用三种形式:
- < 文件名:把文件内容喂给 stdin
- << 结束标记(here-document):在脚本里内嵌多行文本,适合生成配置文件
- <<< '字符串'(here-string):单行字符串快速喂给 stdin
示例 1:< 文件 让 cat 从文件读内容:
# 写法 A:cat 自己打开文件(以参数形式传文件名)
[chenxin@centos7 ~ 19:58:30]$ cat /etc/hosts
# 写法 B:Bash 先打开文件,把内容通过 stdin 喂给 cat
[chenxin@centos7 ~ 19:58:52]$ cat < /etc/hosts
两种写法输出一样,但"谁负责打开文件"的主体不同——写法 B 是标准输入重定向。
示例 2:here-doc(<< EOF)内嵌多行,一次性生成配置文件:
[chenxin@centos7 ~ 20:00:10]$ cat > multi_lines.txt << EOF
> hello world
> hello python
> hello linux
> EOF
[chenxin@centos7 ~ 20:00:32]$ cat multi_lines.txt
hello world
hello python
hello linux





