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