Linux输入输出重定向介绍流程逻辑输入用户通过键盘向stdin输入数据进程从stdin读取这些输入。输出进程将正常结果写入stdout错误信息写入stderr两者默认都显示在终端同时进程也可通过 3 号通道与其他文件进行数据的读写交互。这种模型是 Unix/Linux 系统中 “一切皆文件” 思想的体现让进程能以统一的方式处理不同来源 / 目标的数据也为重定向Redirect等操作如把stdout输出到文件、把文件内容作为stdin输入提供了基础。标准输出重定向到文件#查找 /etc 目录下 文件名是passwd的所有文件[cywcentos7 ~15:32:25]$find/etc/-namepasswdfind: ‘/etc/grub.d’: 权限不够 find: ‘/etc/pki/CA/private’: 权限不够 find: ‘/etc/pki/rsyslog’: 权限不够 /etc/pam.d/passwd find: ‘/etc/dhcp’: 权限不够 /etc/passwd...# 将找到的文件名清单覆盖写入到passwd.list文件中[cywcentos7 ~15:33:07]$find/etc/-namepasswd1passwd,list find: ‘/etc/grub.d’: 权限不够 find: ‘/etc/pki/CA/private’: 权限不够 find: ‘/etc/pki/rsyslog’: 权限不够 find: ‘/etc/dhcp’: 权限不够...[cywcentos7 ~15:33:56]$catpasswd.list /etc/pam.d/passwd /etc/passwd# 补充说明# 1passwd.list可简写为passwd.list# 1和之间不能有空格# 将多个文件内容合并为1个[cywcentos7 ~15:34:13]$cat/etc/fstab /etc/hostsallinone# 将找到的文件名清单追加写入到passwd.list文件中[cywcentos7 ~15:35:31]$find/etc/-namepasswd1passwd,list find: ‘/etc/grub.d’: 权限不够 find: ‘/etc/pki/CA/private’: 权限不够 find: ‘/etc/pki/rsyslog’: 权限不够 find: ‘/etc/dhcp’: 权限不够 find: ‘/etc/selinux/targeted/active’: 权限不够 find: ‘/etc/selinux/final’: 权限不够...[cywcentos7 ~15:36:23]$catpasswd,list /etc/pam.d/passwd /etc/passwd /etc/pam.d/passwd /etc/passwd# 将错误信息覆盖写入到passwd.err文件中[cywcentos7 ~15:37:14]$find/etc/-namepasswd2passwd.err /etc/pam.d/passwd /etc/passwd[cywcentos7 ~15:38:45]$catpasswd.err find: ‘/etc/grub.d’: 权限不够 find: ‘/etc/pki/CA/private’: 权限不够 find: ‘/etc/pki/rsyslog’: 权限不够 find: ‘/etc/dhcp’: 权限不够 find: ‘/etc/selinux/targeted/active’: 权限不够...# 将错误信息扔掉[cywcentos7 ~15:39:15]$find/etc/-namepasswd2/dev/null /etc/pam.d/passwd /etc/passwd# 将错误信息追加写入到passwd.err文件中[cywcentos7 ~15:39:47]$find/etc/-namepasswd2passwd.err /etc/pam.d/passwd /etc/passwd[cywcentos7 ~15:40:42]$catpasswd.err find: ‘/etc/grub.d’: 权限不够 find: ‘/etc/pki/CA/private’: 权限不够 find: ‘/etc/pki/rsyslog’: 权限不够 find: ‘/etc/dhcp’: 权限不够 find: ‘/etc/selinux/targeted/active’: 权限不够 find: ‘/etc/selinux/final’: 权限不够....# 将错误信息、正确信息 覆盖 写入到不同文件[cywcentos7 ~15:40:59]$find/etc/-namepasswdpasswd,list2passwd.err# 将错误信息、正确信息 追加 写入到不同文件[cywcentos7 ~15:42:25]$find/etc/-namepasswdpasswd,list2passwd.err# 用于清空文件内容也可用于创建一个空文件testfile[cywcentos7 ~15:42:43]$testfile# 如果不存在则创建一个文件如果文件存在则啥也不干[cywcentos7 ~15:43:17]$testfile# 同时将错误信息、正确信息 覆盖 写入到相同文件[cywcentos7 ~15:44:51]$find/etc/-namepasswdpasswd.all# 同时将错误信息、正确信息 追加 写入到相同文件[cywcentos7 ~15:46:00]$find/etc/-namepasswdpasswd.all1和之间千万不能有空格否则会出错标准输出重定向到程序管道符|可以将上一个命令的输出作为标准输入传递给下一个命令#查看ext4中含有resize的行# 搜索所有ext4相关手册并保存到ext4.man文件中[cywcentos7 ~15:48:05]$man-kext4exte.man#过滤ext4.man文件中含有resize的行[cywcentos7 ~16:16:02]$grepresize exte.man resize2fs(8)- ext2/ext3/ext4filesystem resizer# 以上两个命令可以和合并为一行[cywcentos7 ~16:17:02]$man-kext4|grepresizer resize2fs(8)- ext2/ext3/ext4filesystem resizer#获取ens33网卡ip地址[cywcentos7 ~16:17:30]$ipaddr|grepens33$|awk{print $2}|cut-d/-f110.1.8.10#注意管道符号后的命令必须能够接收标准输入例如 ls **不**接收管道传递过来的标准输入[cywcentos7 ~16:19:42]$echo/etc/passwd|ls-l总用量24-rw-rw-r--.1cyw cyw6997月1615:34 allinone -rw-r--r--.1cyw cyw3677月1614:47 dhcpd.conf -rw-rw-r--.1cyw cyw12947月1616:17 exte.man lrwxrwxrwx.1cyw cyw97月1519:24 mytmp -/var/tmp/ -rw-rw-r--.1cyw cyw15947月1615:46 passwd.all -rw-rw-r--.1cyw cyw07月1615:44 passwd.err -rw-rw-r--.1cyw cyw07月1615:44 passwd.list -rw-r--r--.1cyw cyw18817月1611:23 profile -rw-r--r--.1cyw cyw18687月1613:42 profile.sh -rw-rw-r--.1cyw cyw07月1615:43 testfile drwxr-xr-x.2cyw cyw67月1410:59 公共 drwxr-xr-x.2cyw cyw67月1410:59 模板 drwxr-xr-x.2cyw cyw67月1410:59 视频 drwxr-xr-x.2cyw cyw67月1410:59 图片 drwxr-xr-x.2cyw cyw67月1410:59 文档 drwxr-xr-x.2cyw cyw67月1410:59 下载 drwxr-xr-x.2cyw cyw67月1410:59 音乐 drwxr-xr-x.2cyw cyw67月1411:08 桌面补充#先创建文件 file-{01..03}再查看[cywcentos7 ~16:31:56]$touchfile-{01..03}[cywcentos7 ~16:33:59]$ls-lfile-{01..06}ls: 无法访问file-04: 没有那个文件或目录 ls: 无法访问file-05: 没有那个文件或目录 ls: 无法访问file-06: 没有那个文件或目录 -rw-rw-r--.1cyw cyw07月1616:33 file-01 -rw-rw-r--.1cyw cyw07月1616:33 file-02 -rw-rw-r--.1cyw cyw07月1616:33 file-03#管道过滤不存在的文件file-06[cywcentos7 ~16:34:15]$ls-lfile-{01..06}|grepfile-06 ls: 无法访问file-04: 没有那个文件或目录 ls: 无法访问file-05: 没有那个文件或目录 ls: 无法访问file-06: 没有那个文件或目录#管道过滤存在的文件file-03[cywcentos7 ~16:35:10]$ls-lfile-{01..06}|grepfile-03 ls: 无法访问file-04: 没有那个文件或目录 ls: 无法访问file-05: 没有那个文件或目录 ls: 无法访问file-06: 没有那个文件或目录 -rw-rw-r--.1cyw cyw07月1616:33 file-03# | 标准错误和标准输出同时传递给下一个命令作为下一个命令的标准输入[cywcentos7 ~16:35:27]$ls-lfile-{01..06}|grepfile-06 ls: 无法访问file-06: 没有那个文件或目录tee命令作用接收管道传递过来的stdin并继续作为stdout输出#清空文件[cywcentos7 ~16:20:33]$passwd.list# 前没有任何东西就会将后面文件的内容覆盖为空[cywcentos7 ~16:21:36]$catpasswd.list[cywcentos7 ~16:21:43]$#文件没有内容# tee命令覆盖保存到文件[cywcentos7 ~16:21:45]$find/etc/-namepasswd2/dev/null|teepasswd.list#file2覆盖到/dev/nullfile1覆盖保存到passwd.list/etc/pam.d/passwd /etc/passwd[cywcentos7 ~16:22:41]$catpasswd.list /etc/pam.d/passwd /etc/passwd# tee命令追加保存到文件[cywcentos7 ~16:28:04]$find/etc-namepasswd2/dev/null|tee-apasswd.list#tee -a :追加保存作用等同于/etc/pam.d/passwd /etc/passwd[cywcentos7 ~16:29:49]$catpasswd.list /etc/pam.d/passwd /etc/passwd /etc/pam.d/passwd /etc/passwd# tee的标准输出继续通过管道传递下去[cywcentos7 ~16:26:02]$man-kpasswd|teepasswd.man|greppasswdchgpasswd(8)- 批量更新组密码 chpasswd(8)- 批量更新密码 gpasswd(1)- administer /etc/group and /etc/gshadow mkpasswd(1)- 为用户产生新口令passwd(5)-(未知的主题)...[cywcentos7 ~16:27:32]$catpasswd.man chgpasswd(8)- 批量更新组密码 chpasswd(8)- 批量更新密码 gpasswd(1)- administer /etc/group and /etc/gshadow mkpasswd(1)- 为用户产生新口令passwd(5)-(未知的主题)...标准输入重定向给程序#常规查看文件[cywcentos7 ~16:44:21]$cat/etc/hosts127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6# cat 命令标准输入数据[cywcentos7 ~16:43:30]$cat/etc/hosts#数据流127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6[cywcentos7 ~16:48:13]$cat#只输入cat,回车hello#输入内容回车hello#显示输入的内容按ctrld退出# shell读取到EOF时候代表标准输入完成[cywcentos7 ~16:58:43]$catEOFhellohegiuajeEOF#按回车后自动结束并退出显示输入内容hello hegiu aje判定一个命令是否接收标准输入最简单的方式就是将文件内容作为标准输入重定向给程序#一次性将多行内容写入到文件中# 方法1使用引号[cywcentos7 ~17:00:52]$ ^C[cywcentos7 ~17:00:56]$echohello world hello python hello linuxlines.txt[cywcentos7 ~17:05:32]$catlines.txt hello world hello python hello linux#方法2使用cat重定向(推荐使用)[cywcentos7 ~17:05:44]$catlines.txtEOFhellohello worldhello linuxEOF hello hello world hello linux[cywcentos7 ~17:07:11]$catlines.txt hello world hello python hello linux