博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
最近学的linux重定向命令
阅读量:7143 次
发布时间:2019-06-29

本文共 1954 字,大约阅读时间需要 6 分钟。

标准输出命令字符 其实是 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

转载于:https://blog.51cto.com/14167037/2342058

你可能感兴趣的文章
VS201“.NET研究”0实践RUP4+1架构模型
查看>>
两级导航栏联动效果
查看>>
php ftp类
查看>>
VK Cup 2012 Qualification Round 1 A. Next Round
查看>>
(译)cocos2d精灵教程:第二部分
查看>>
高校女生坐等强女干。保安称压力大。。【转】
查看>>
Python 版 Instance Activator
查看>>
分享30套免费的高质量网页按钮素材
查看>>
亲测SQLServer的最大连接数 (转)
查看>>
typeof 运算符用法
查看>>
SignalR 0.5 发布了
查看>>
批量照片缩小器展示多线程控件BackgroundWorker后台工作使用方法
查看>>
HTML5之Canvas绘图——Canvas画布调整之移动、缩放、旋转
查看>>
排查数据库性能的常用sql语句
查看>>
全排列
查看>>
Node.js&NPM的安装与配置(转)
查看>>
C# CRC16 查表法
查看>>
js中获取键盘事件
查看>>
面试(4)-spring-Spring面试题和答案
查看>>
请教 JTable 里的单元格如何使得双击进入单元格后,单元格的内容处于全选中状态...
查看>>