IO重定向和管道符
编辑
1
2025-10-10

IO重定向和管道符
简单记录一下IO重定向和管道符,这个很酷。
一、IO重定向
1.1 文件描述符
Linux一切皆文件。
到处都在说,Linux一切皆文件,甚至,键盘输入也是一个文件、屏幕输出也是文件,屏幕上的错误输出也是文件。
0
标准输入1
标准输出2
标准错误输出
1.2 输出重定向
1.2.1 覆盖重定向 >
创建新文件(如文件已经存在,会覆盖掉源文件)
# 创建新文件
> 文件名
# 将程序输出文件,而不是屏幕
命令 > 文件名
# 创建新文件
[root@centos9 ~]# ls
anaconda-ks.cfg
[root@centos9 ~]# > test.txt
[root@centos9 ~]# ls
anaconda-ks.cfg test.txt
[root@centos9 ~]#
# 将程序输出文件,而不是屏幕
[root@centos9 ~]# date > date.txt
[root@centos9 ~]# cat date.txt
Fri Oct 10 05:27:06 PM CST 2025
[root@centos9 ~]#
1.2.1 追加重定向 >>
如文件已经存在,会将命令结果输出追加到文件末尾。如文件还不存在,会创建新文件
命令 >> 文件名
# 追加前查看
[root@centos9 ~]# cat date.txt
Fri Oct 10 05:27:06 PM CST 2025
[root@centos9 ~]#
# 追加
[root@centos9 ~]# date >> date.txt
[root@centos9 ~]#
# 追加后查看
[root@centos9 ~]# cat date.txt
Fri Oct 10 05:27:06 PM CST 2025
Fri Oct 10 05:41:13 PM CST 2025
[root@centos9 ~]#
1.3 输出重定向案例
1.3.1 find排除错误输出
有些情况,会有报错输出,影响阅读。可以重定向错误输出到 /dev/null
或文件中
命令 2> /dev/null
命令 2> error.txt
find / -size +50M 2> /dev/null
find / -size +50M 2> error.txt
例如,find
查找大于50M文件。
[root@centos9 ~]# find / -size +50M
/boot/initramfs-0-rescue-7ed357ca5a7e42419dd56d6e3dbb1f24.img
/proc/kcore
find: ‘/proc/1961/task/1961/fd/6’: No such file or directory
find: ‘/proc/1961/task/1961/fdinfo/6’: No such file or directory
find: ‘/proc/1961/fd/5’: No such file or directory
find: ‘/proc/1961/fdinfo/5’: No such file or directory
/sys/devices/pci0000:00/0000:00:0f.0/resource1
/sys/devices/pci0000:00/0000:00:0f.0/resource1_wc
[root@centos9 ~]#
/dev/null
类似于垃圾桶,直接丢弃
[root@centos9 ~]# find / -size +50M 2> /dev/null
/boot/initramfs-0-rescue-7ed357ca5a7e42419dd56d6e3dbb1f24.img
/proc/kcore
/sys/devices/pci0000:00/0000:00:0f.0/resource1
/sys/devices/pci0000:00/0000:00:0f.0/resource1_wc
[root@centos9 ~]#
# 错误信息输出到文件中
[root@centos9 ~]# find / -size +50M 2> error.txt
/boot/initramfs-0-rescue-7ed357ca5a7e42419dd56d6e3dbb1f24.img
/proc/kcore
/sys/devices/pci0000:00/0000:00:0f.0/resource1
/sys/devices/pci0000:00/0000:00:0f.0/resource1_wc
[root@centos9 ~]#
# 查看文件中的错误输出
[root@centos9 ~]# cat error.txt
find: ‘/proc/2008/task/2008/fd/6’: No such file or directory
find: ‘/proc/2008/task/2008/fdinfo/6’: No such file or directory
find: ‘/proc/2008/fd/5’: No such file or directory
find: ‘/proc/2008/fdinfo/5’: No such file or directory
[root@centos9 ~]#
1.3.2 标准输出/标准错误分开重定向
命令 > correct.txt 2> error.txt
# 分开重定向
[root@centos9 ~]# find / -size +50M > correct.txt 2> error.txt
# 查看正确输出
[root@centos9 ~]# cat correct.txt
/boot/initramfs-0-rescue-7ed357ca5a7e42419dd56d6e3dbb1f24.img
/proc/kcore
/sys/devices/pci0000:00/0000:00:0f.0/resource1
/sys/devices/pci0000:00/0000:00:0f.0/resource1_wc
# 查看标准错误输出
[root@centos9 ~]# cat error.txt
find: ‘/proc/1997/task/1997/fd/6’: No such file or directory
find: ‘/proc/1997/task/1997/fdinfo/6’: No such file or directory
find: ‘/proc/1997/fd/5’: No such file or directory
find: ‘/proc/1997/fdinfo/5’: No such file or directory
[root@centos9 ~]#
1.3.3 合并重定向
法一,混合重定向
命令 &> all.txt
[root@centos9 ~]# find / -size +50M &> all.txt
[root@centos9 ~]# cat all.txt
/boot/initramfs-0-rescue-7ed357ca5a7e42419dd56d6e3dbb1f24.img
/proc/kcore
find: ‘/proc/2001/task/2001/fd/6’: No such file or directory
find: ‘/proc/2001/task/2001/fdinfo/6’: No such file or directory
find: ‘/proc/2001/fd/5’: No such file or directory
find: ‘/proc/2001/fdinfo/5’: No such file or directory
/sys/devices/pci0000:00/0000:00:0f.0/resource1
/sys/devices/pci0000:00/0000:00:0f.0/resource1_wc
[root@centos9 ~]#
法二,混合重定向,标准错误重定向到标准输出。
命令 > all.txt 2>&1
[root@centos9 ~]# find / -size +50M > all.txt 2>&1
[root@centos9 ~]# cat all.txt
/boot/initramfs-0-rescue-7ed357ca5a7e42419dd56d6e3dbb1f24.img
/proc/kcore
find: ‘/proc/2005/task/2005/fd/6’: No such file or directory
find: ‘/proc/2005/task/2005/fdinfo/6’: No such file or directory
find: ‘/proc/2005/fd/5’: No such file or directory
find: ‘/proc/2005/fdinfo/5’: No such file or directory
/sys/devices/pci0000:00/0000:00:0f.0/resource1
/sys/devices/pci0000:00/0000:00:0f.0/resource1_wc
[root@centos9 ~]#
1.4 输入重定向
1.4.1 文件重定向 <
有时候需要从文件中读取内容,作为命令的参数。
- 查看文件内容
[root@centos9 ~]# cat < /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
[root@centos9 ~]#
- 配置邮件服务
# centos7
yum install mailx
# centos9
dnf install s-nail -y
# centos9配置邮箱
~/.mailrc
set from="你的邮箱@qq.com(昵称)"
set smtp="smtps://smtp.qq.com:465"
set smtp-auth=login
set smtp-auth-user="你的邮箱@qq.com"
set smtp-auth-password="你的授权码"
set ssl-verify=ignore
- 发送邮件
输入重定向文件内容给 mail
mail -s "测试标题" 收件人@example.com < body.txt
[root@centos9 ~]# mail -s "测试标题" **********@qq.com < body.txt
s-nail: Warning: variable superseded or obsoleted: smtp
s-nail: Warning: variable superseded or obsoleted: smtp-auth-user
s-nail: Warning: variable superseded or obsoleted: smtp-auth-password
s-nail: Warning: variable superseded or obsoleted: ssl-verify
s-nail: Obsoletion warning: please do not use *smtp*, instead assign a smtp:// URL to *mta*!
s-nail: Obsoletion warning: Use of old-style credentials, which will vanish in v15!
s-nail: Please read the manual section "On URL syntax and credential lookup"
[root@centos9 ~]#
1.4.2 文件终止符 <<
创建并写入文件内容,常用于脚本中。
cat > 文件 << EOF
行1
行2
行3
EOF
# 写入文件内容
[root@centos9 ~]# cat > file.txt << EOF
> 11111
> 2222222
> 33333333333
> 44444444
> EOF
# 验证写入的内容
[root@centos9 ~]# cat file.txt
11111
2222222
33333333333
44444444
[root@centos9 ~]#
1.4.3 字符输入 <<<
预估脚本交互式界面,需要输入一些字符时,可以这么做。
命令 <<< 期望命令执行过程中需要输入的字符
对照组。
# 对照
[root@centos9 ~]# yum install vim
Last metadata expiration check: 0:36:30 ago on Fri 10 Oct 2025 06:37:33 PM CST.
Dependencies resolved.
============================================================================================
Package Architecture Version Repository Size
============================================================================================
Installing:
vim-enhanced x86_64 2:8.2.2637-22.el9 appstream 1.7 M
Installing dependencies:
gpm-libs x86_64 1.20.7-29.el9 appstream 21 k
vim-common x86_64 2:8.2.2637-22.el9 appstream 7.0 M
vim-filesystem noarch 2:8.2.2637-22.el9 baseos 13 k
Transaction Summary
=============================================================================================
Install 4 Packages
Total download size: 8.8 M
Installed size: 34 M
Is this ok [y/N]:
# 字符输入
[root@centos9 ~]# yum install vim <<< y
Last metadata expiration check: 0:39:04 ago on Fri 10 Oct 2025 06:37:33 PM CST.
Dependencies resolved.
====================================================================================================
Package Architecture Version Repository Size
====================================================================================================
Installing:
vim-enhanced x86_64 2:8.2.2637-22.el9 appstream 1.7 M
Installing dependencies:
gpm-libs x86_64 1.20.7-29.el9 appstream 21 k
vim-common x86_64 2:8.2.2637-22.el9 appstream 7.0 M
vim-filesystem noarch 2:8.2.2637-22.el9 baseos 13 k
Transaction Summary
====================================================================================================
Install 4 Packages
Total download size: 8.8 M
Installed size: 34 M
Is this ok [y/N]: Downloading Packages:
(1/4): gpm-libs-1.20.7-29.el9.x86_64.rpm 33 kB/s | 21 kB 00:00
(2/4): vim-enhanced-8.2.2637-22.el9.x86_64.rpm 906 kB/s | 1.7 MB 00:01
(3/4): vim-filesystem-8.2.2637-22.el9.noarch.rpm 4.5 kB/s | 13 kB 00:02
(4/4): vim-common-8.2.2637-22.el9.x86_64.rpm 778 kB/s | 7.0 MB 00:09
-----------------------------------------------------------------------------------------------------
Total 771 kB/s | 8.8 MB 00:11
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : gpm-libs-1.20.7-29.el9.x86_64 1/4
Installing : vim-filesystem-2:8.2.2637-22.el9.noarch 2/4
Installing : vim-common-2:8.2.2637-22.el9.x86_64 3/4
Installing : vim-enhanced-2:8.2.2637-22.el9.x86_64 4/4
Running scriptlet: vim-enhanced-2:8.2.2637-22.el9.x86_64 4/4
Verifying : vim-filesystem-2:8.2.2637-22.el9.noarch 1/4
Verifying : gpm-libs-1.20.7-29.el9.x86_64 2/4
Verifying : vim-common-2:8.2.2637-22.el9.x86_64 3/4
Verifying : vim-enhanced-2:8.2.2637-22.el9.x86_64 4/4
Installed:
gpm-libs-1.20.7-29.el9.x86_64
vim-common-2:8.2.2637-22.el9.x86_64
vim-enhanced-2:8.2.2637-22.el9.x86_64
vim-filesystem-2:8.2.2637-22.el9.noarch
Complete!
[root@centos9 ~]#
二、管道符 |
管道符,前面命令的标准输出,作为参数传入到管道右边的命令。
命令1 | 命令2
2.1 常见案例
- 过滤文件信息
cat /etc/passwd |grep root
[root@centos9 ~]# cat /etc/passwd |grep root
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@centos9 ~]#
- 查找某个进程
ps aux |grep firewalld
[root@centos9 ~]# ps aux |grep firewalld
root 691 0.0 2.2 125072 40140 ? Ssl 11:25 0:00 /usr/bin/python3 -s /usr/sbin/firewalld --nofork --nopid
root 11813 0.0 0.1 6400 2244 pts/1 S+ 19:24 0:00 grep --color=auto firewalld
[root@centos9 ~]#
- 修改密码
echo "用户名:密码" | sudo chpasswd
[root@centos9 ~]# echo "qiankong:1234567" | sudo chpasswd
[root@centos9 ~]#
- centos系列系统改密码
echo "newpassword" | sudo passwd --stdin username
2.2 分流 tee
tee,将标准输出通过管道后,分流(镜像)出一份,输出到屏幕或文件中。
命令 | tee 文件
命令 | tee -a 文件
- tee 后面什么也没有,代表输出到屏幕
- tee 后面的参数会作为文件
-a
追加到文件中
命令1 | 命令2 | 命令3 | 命令4 | 命令5
对于一长串的命令,为了调试,想要查看某个中间过程的输出,只需要插入 tee 文件名称
即可。
命令1 | 命令2 | tee 命令2输出.log | 命令3 | 命令4 | 命令5
三、封面图
- 0
- 0
-
分享