linux重定向命令

命令解释
> 和 >1标准输出重定向,先清空内容,然后写入文件中
>> 和 1>>标准输出追加重定向,把内容追加到文件中
2>标准错误重定向,先清空内容,然后把错误提示写入文件中
2>>标准错误追加重定向,把错误提示追加到文件中
< 或 0<标准输入重定向,用于 xargs 、tr 这两个命令
<< 或 0<<标准输入追加重定向

>和>>

他们俩其实唯一的区别就是>是重定向到一个文件,>>是追加内容到文件。两个命令都是如果文件不存在则创建文件,但是目录不存在就会报错。举个例子来看看:

先用这个创建一个文件,查看文件内容显示123123123
再用>重定向到这个文件查看,可以发现文件内容改变了,这就是把之前的清空了然后添加新的内容
再用>>追加重定向添加一行内容,cat查看就可以看到那两行了,之前的内容不会消失,只会追加到最后一行

[root@shell ~ 48]# echo '123123123' > test.txt
[root@shell ~ 49]# cat test.txt 
123123123
[root@shell ~ 50]# echo '!!!!!!!!' >test.txt 
[root@shell ~ 51]# cat test.txt 
!!!!!!!!
[root@shell ~ 52]# echo "--------" >>test.txt 
[root@shell ~ 53]# cat test.txt 
!!!!!!!!
--------

2>> 将错误的信息输出到文件里

注意:0 是标准输入(STDIN),1 是标准输出(STDOUT),2 是标准错误输出(STDERR)。
把内存信息输出到文件里

[root@shell ~ 58]# free -h 1> test.txt 
[root@shell ~ 59]# cat test.txt 
              total        used        free      shared  buff/cache   available
Mem:           976M        116M        411M        6.7M        448M        673M
Swap:          2.0G          0B        2.0G

这里的 2和 > 之间不可以有空格,2> 是一体的时候才表示错误输出。
随便输入字母,没有这个命令,把这个错误语句输出到文件里要加上2

[root@shell ~ 69]# dagasd 2> test.txt 
[root@shell ~ 70]# cat test.txt 
-bash: dagasd: command not found
[root@shell ~ 71]# cccccccc 2>> test.txt 
[root@shell ~ 72]# cat test.txt 
-bash: dagasd: command not found
-bash: cccccccc: command not found

1>>test.txt 2>>test-error.txt 将错误提示和正确信息分开存放

#先随便查看一个不存在的文件然后写入到错误文件内

[root@shell ~ 79]# cat sdgdg 1>>test.txt 2>>test-error.txt

[root@shell ~ 81]# cat test.txt    ##  这个文件还是之前的,没有变化
-bash: dagasd: command not found
-bash: cccccccc: command not found

[root@shell ~ 82]# cat test-error.txt   ##   把刚才查看的错误语句追加到了这个文件中
cat: sdgdg: No such file or directory

##在测试一下正确返回的命令,查看ip然后追加到正确的文件夹里

[root@shell ~ 83]# ip a 1>>test.txt 2>>test-error.txt
[root@shell ~ 84]# cat test.txt 
-bash: dagasd: command not found
-bash: cccccccc: command not found 			

##  这里是之前的两行测试,下面的是刚追加的

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:54:7d:6e brd ff:ff:ff:ff:ff:ff
    inet 192.168.100.20/24 brd 192.168.100.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::11da:976f:c32b:2be2/64 scope link 
       valid_lft forever preferred_lft forever

1>>shell.txt 2>&1 将错误提示和正确信息写入一个文件内

这个适用于命令、脚本、、程序执行过程中的提示(正确\错误)都记录下来

如果像刚才那样也可以,把两个文件名字改成一个就好了,但是那样写的会比较麻烦,所以就用一个简单点的办法来实现
2>&1 一定是一个>号,两个会报错
2>&1 是把错误的信息放到1里面

[root@shell ~ 85]# kdasdhh 1>>shell.txt 2>&1
[root@shell ~ 86]# cat shell.txt 
-bash: kdasdhh: command not found
[root@shell ~ 87]# free -h 1>>shell.txt 2>&1
[root@shell ~ 88]# cat shell.txt 
-bash: kdasdhh: command not found
              total        used        free      shared  buff/cache   available
Mem:           976M        116M        411M        6.7M        448M        673M
Swap:          2.0G          0B        2.0G
[root@shell ~ 89]# 111 1>>shell.txt 2>&1
[root@shell ~ 90]# cat shell.txt 
-bash: kdasdhh: command not found
              total        used        free      shared  buff/cache   available
Mem:           976M        116M        411M        6.7M        448M        673M
Swap:          2.0G          0B        2.0G
-bash: 111: command not found

< 标准输入重定向

用echo输出10个序列,然后写入到文件内
查看一下一行10个数字
用xargs -n2 让他从文件内显示到屏幕上一行两列分组

[root@shell ~ 93]# echo {01..10}
01 02 03 04 05 06 07 08 09 10
[root@shell ~ 94]# echo {01..10} > shell.txt 
[root@shell ~ 95]# cat shell.txt 
01 02 03 04 05 06 07 08 09 10
[root@shell ~ 96]# xargs -n2 <shell.txt 
01 02
03 04
05 06
07 08
09 10

<< 标准输入追加重定向

这个用的次数很多,常用命令
批量追加

[root@shell ~ 97]# cat >>shell.txt <<EOF
> 11
> !!!!!!!!!!!!!!
> ########################
> dasgdasg
> ..........??????????
> EOF
[root@shell ~ 98]# cat shell.txt 
01 02 03 04 05 06 07 08 09 10
11
!!!!!!!!!!!!!!
########################
dasgdasg
..........??????????
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值