8.5 输入输出重定向

大于号,重定向

> 正确输出
>> 追加重定向
2> 错误重定向
2>> 错误追加重定向

>+2>等于&>        表示结合了正确和错误
  • cat 1.txt > 2.txt
    • 一个大于号表示正确的输出
    • 大于号>,表示前面的命令输出,直接输入到后面的文件中去
    • 就会把1.txt文件内容重定向到2.txt文件中,而2.txt之前的文件就会删除掉,重新写入1.txt文件内容
  • cat 1.txt >> 2.txt
    • 两个大于号>>,就是追加,不会删除2.txt文件内容,而是在原有的基础上将1.txt文件内容写入到2.txt文件中去
  • ls aaa.txt 2> err
    • 2大于号表示错误的输出(错误信息)
    • 2> 表示它会把命令产生的错误信息指定输入到一个文件中去
[root@hf-01 ~]# laaa
-bash: laaa: 未找到命令
[root@hf-01 ~]# laaa 2> a.txt
[root@hf-01 ~]# cat a.txt
-bash: laaa: 未找到命令
[root@hf-01 ~]# 
  • ls aaa.txt 2>> err
  • ls [12].txt aaa.txt &> a.txt //正确和错误的输出信息都输出到a.txt中
[root@hf-01 ~]# ls [12].txt aaa.txt &> a.txt
[root@hf-01 ~]# cat a.txt
ls: 无法访问aaa.txt: 没有那个文件或目录
1.txt
2.txt
[root@hf-01 ~]# 
  • 把正确和错误的输出到文件中,方法一 ls [12].txt aaa.txt &>> a.txt
[root@hf-01 ~]# ls [12].txt aaa.txt &>> a.txt
[root@hf-01 ~]# cat !$
cat a.txt
ls: 无法访问aaa.txt: 没有那个文件或目录
1.txt
2.txt
ls: 无法访问aaa.txt: 没有那个文件或目录
1.txt
2.txt
  • 把正确和错误的输出到文件中,方法二 ls [12].txt aaa.txt >1.txt 2>a.txt
[root@hf-01 ~]# ls [12].txt aaa.txt >1.txt 2>a.txt
[root@hf-01 ~]# cat 1.txt
1.txt
2.txt
[root@hf-01 ~]# cat a.txt
ls: 无法访问aaa.txt: 没有那个文件或目录
既可以写入一个文件中,也可以分开写入

小于号,重定向

  • 小于号< ,输入重定向
  • wc -l < 1.txt //把1.txt文件内容输入重定向到命令wc -l 中去
[root@hf-01 ~]# wc -l < 1.txt
2
[root@hf-01 ~]# 2.txt < 1.txt
-bash: 2.txt: 未找到命令
[root@hf-01 ~]# 
  • 输入重定向,左边必须是命令,不支持文件输入重定向到文件中的

转载于:https://my.oschina.net/u/3707314/blog/1573780

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值