最近学的linux重定向命令

标准输出命令字符 其实是 1> ,1可以省略
两个终端连接在一台主机上,终端分别为 1 2
假设在/data下有文件F1.log
1:ls > 2
2:F1.log
主要是将内容重定向至文件中,如果没有文件,就生成,有就覆盖
ls > /data/f1
cat /data/f1
F1.log
ls >> /data/f1 当不想覆盖文件时,再加一个 >
cat /data/f1
F1.log
F1.log
f1

set -C 禁止覆盖
| 加入| 符号,在set -C 下也可以强制覆盖
set +C 覆盖

2> 标准错误命 令字符
当错误信息使用了标准输出字符时,会清空文件内容
[root@Centos7 ~]#cat /data/ls.out
anaconda-ks.cfg
Desktop
Documents
Downloads
f1link
f1link2
file
initial-setup-ks.cfg
Music
Pictures
Public
Templates
Videos
[root@Centos7 ~]#errcmd > /data/ls.out
bash: errcmd: command not found...
[root@Centos7 ~]#cat /data/ls.out
这一段为无内容
下一段是标准错误的命令
[root@Centos7 ~]#ls > ls.out
[root@Centos7 ~]#cat ls.out
anaconda-ks.cfg
Desktop
Documents
Downloads
f1link
f1link2
file
initial-setup-ks.cfg
ls.out
Music
Pictures
Public
Templates
Videos
[root@Centos7 ~]#errcmd 2> ls.out
[root@Centos7 ~]#cat ls.out
bash: errcmd: command not found...
同样,不想覆盖文件,多加一个> 即是 2>>

可以同时执行两个命令
[root@Centos7 ~]#ls /etc/centos-release /etc/f1.out
ls: cannot access /etc/f1.out: No such file or directory
/etc/centos-release
[root@Centos7 ~]#ls /etc/centos-release /etc/f1.out > /data/3.log 2> /data/4.log
[root@Centos7 ~]#cat /data/3.log
/etc/centos-release
[root@Centos7 ~]#cat /data/4.log
ls: cannot access /etc/f1.out: No such file or directory

将两个信息放在一个文件的两个写法
第一个写法更为方便,时间比较后出现,相对较新
[root@Centos7 ~]#ls /etc/centos-release /etc/f1.out &> /data/all.log
[root@Centos7 ~]#cat /data/all.log
ls: cannot access /etc/f1.out: No such file or directory
/etc/centos-release
第二个是传统写法
[root@Centos7 ~]#ls /etc/centos-release /etc/f1.out > /data/all2.log 2>&1
[root@Centos7 ~]#cat /data/all2.log
ls: cannot access /etc/f1.out: No such file or directory
/etc/centos-release

tr 标准输入的一种
tr 可以做字符串转换的命令
tr 'a-z' 'A-Z' 将输入的字母小写的输出为大写的

< 标准输入的重定向
cat f1
abcd
tr 'a-z' 'A-Z' < f1 (即将f1的内容输入执行前面的tr命令
ABCD (仅仅只是显示,可以加入 > 命令将显示内容放在文件
tr 'a-z' 'A-Z' < f1 > f1 (这样会清空文件f1,应该将内容重定向至另一个文件中f2
tr 'a-z' 'A-Z' < f1 > f2
cat f2
ABCD

猜你喜欢

转载自blog.51cto.com/14167037/2342058