重定向和追加
输出重定向
格式: 命令 操作符号 文件名
参数:
1、 1> 或 > 把STDOUT重定向到文件
2、 2> 把STDERR重定向到文件
3、 &> 把标准输出和错误都重定向
4、 >& 和上面功能一样,建议使用上面方式
举例 :把主机名写入1.txt
root@centos7 test]# honstname > 1.txt
[root@centos7 test]# cat 1.txt
centos7.lixiangshuai
标准输出和错误输出各自定向至不同位置
COMMAND > /path/to/file.out 2> /path/to/error.out
以上如果文件已存在,文件内容会被覆盖
set -C 禁止将内容覆盖已有文件,但可追加, 利用 >| 仍可强制覆盖
set +C 允许覆盖,默认
追加
>> 可以在原有内容基础上,追加内容
把输出和错误重新定向追加到文件
>> 追加标准输出重定向至文件
2>> 追加标准错误重定向至文件
举例:把当前目录内的文件名追加到1.txt 中
[root@centos7 test]# cat 1.txt
centos7.lixiangshuai
[root@centos7 test]# ls >> 1.txt
[root@centos7 test]# cat 1.txt
centos7.lixiangshuai
1.taxt
1.txt
2.txt
3.txt
f1.img
f2.img
f3.img
fi.img
[root@centos7 test]#
输入重定向
格式:
COMMAND 0< FILE
COMMAND < FILE
举例
[root@centos7 test]# ls < 2.txt
1.taxt 1.txt 2.txt 3.txt f1.img f2.img f3.img fi.img
[root@centos7 test]# cat 2.txt
[root@centos7 test]#
[root@centos7 test]# ls < 2.txt > 1.txt
[root@centos7 test]# cat 1.txt
1.taxt
1.txt
2.txt
3.txt
f1.img
f2.img
f3.img
fi.img
[root@centos7 test]#
举例2:
[root@centos7 test]# cat 2.txt
abc
[root@centos7 test]# tr 'a-z' 'A-Z' < 2.txt
ABC
[root@centos7 test]# cat 2.txt
abc
[root@centos7 test]#
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?