当前位置:网站首页>redirect action
redirect action
2022-08-09 20:15:00 【Sun Xiaoshen】
一、将输出重定向到文件或程序.
用法 | 说明 |
---|---|
> file | 重定向标准输出(stdout);以覆盖文件 |
>> file | 重定向标准输出(stdout);To attached to the file |
2> file | 重定向标准输入(stderr);以覆盖文件 |
2> /dev/null | 将标准输入(stderr)错误消息重定向到/dev/null,Thus will be discarded him |
&> file 或 > file 2>&1 | 重定向标准输出(stdout)和标准输入(stderr)To cover the same file |
&>> file 或 >> file 2>&1 | 重定向标准输出(stdout)和标准输入(stderr)In additional to the same file |
注意:Redirect file sequence of operation is very important
> file 2>&1
将标准输出重定向到file,Then the standard error as a standard output to the same place(file).
2>&1 > file
In contrast to the above,The standard error is redirected to standard output the default location(终端窗口,因此没有任何更改),Only will then standard output redirectionfile.
示例
1、使用dateCommand to view the timestamp saved to/tmp/dateback中,供日后参考.
[[email protected] ~]$ date > /tmp/dateback
[[email protected] ~]$ cat /tmp/dateback
Wed 3 Aug 19:55:43 CST 2022
2、To a log file finally100Line to another file.
[[email protected] ~]$ sudo tail -n 100 /var/log/messages > /tmp/end-100
[[email protected] ~]$ wc -l /tmp/end-100
100 /tmp/end-100
3、将4A file link as1个文件.
[[email protected] log]$ sudo cat lastlog maillog tallylog > /tmp/old-log
4、Hide home directory of the file name and file name list to a file.
[[email protected] ~]$ ls -a > /tmp/my-file
5、Attach the output to the existing file.
[[email protected] ~]$ echo "new line of information" >> /tmp/many-lines
[[email protected] log]$ sudo diff lastlog tallylog >> /tmp/tracking-mode
6、When checking in on ordinary command output terminal,The error is redirected to the file.
[[email protected] ~]$ find /etc -name passwd 2> /tmp/errors
/etc/pam.d/passwd
/etc/passwd
[[email protected] ~]$ cat /tmp/errors
find: ‘/etc/lvm/archive’: Permission denied
find: ‘/etc/lvm/backup’: Permission denied
find: ‘/etc/lvm/cache’: Permission denied
find: ‘/etc/pki/rsyslog’: Permission denied
find: ‘/etc/pki/pesign’: Permission denied
find: ‘/etc/ssh/sshd_config.d’: Permission denied
find: ‘/etc/grub.d’: Permission denied
find: ‘/etc/sssd’: Permission denied
find: ‘/etc/polkit-1/rules.d’: Permission denied
find: ‘/etc/polkit-1/localauthority’: Permission denied
find: ‘/etc/cups/ssl’: Permission denied
find: ‘/etc/cni/net.d’: Permission denied
find: ‘/etc/nftables’: Permission denied
find: ‘/etc/audit’: Permission denied
find: ‘/etc/sudoers.d’: Permission denied
find: ‘/etc/libvirt’: Permission denied
find: ‘/etc/containers/networks’: Permission denied
find: ‘/etc/sos/cleaner’: Permission denied
find: ‘/etc/ipsec.d’: Permission denied
find: ‘/etc/firewalld’: Permission denied
7、Save the process output to/tmp/output文件中,To save the error message to/tmp/errors文件中.
[[email protected] ~]$ find /etc -name passwd > /tmp/output 2> /tmp/errors
[[email protected] ~]$ cat /tmp/output
/etc/pam.d/passwd
/etc/passwd
[[email protected] ~]$ cat /tmp/errors
find: ‘/etc/lvm/archive’: Permission denied
find: ‘/etc/lvm/backup’: Permission denied
find: ‘/etc/lvm/cache’: Permission denied
find: ‘/etc/pki/rsyslog’: Permission denied
find: ‘/etc/pki/pesign’: Permission denied
find: ‘/etc/ssh/sshd_config.d’: Permission denied
find: ‘/etc/grub.d’: Permission denied
find: ‘/etc/sssd’: Permission denied
find: ‘/etc/polkit-1/rules.d’: Permission denied
find: ‘/etc/polkit-1/localauthority’: Permission denied
find: ‘/etc/cups/ssl’: Permission denied
find: ‘/etc/cni/net.d’: Permission denied
find: ‘/etc/nftables’: Permission denied
find: ‘/etc/audit’: Permission denied
find: ‘/etc/sudoers.d’: Permission denied
find: ‘/etc/libvirt’: Permission denied
find: ‘/etc/containers/networks’: Permission denied
find: ‘/etc/sos/cleaner’: Permission denied
find: ‘/etc/ipsec.d’: Permission denied
find: ‘/etc/firewalld’: Permission denied
8、忽略并丢弃错误消息,But save the process output message.
[[email protected] ~]$ find /etc -name passwd > /tmp/export 2> /dev/null
[[email protected] ~]$ cat /tmp/export
/etc/pam.d/passwd
/etc/passwd
[[email protected] ~]$ cat /dev/null
9、The output and generate error messages stored together.
[[email protected] ~]$ find /etc -name passwd &> /tmp/save-both
[[email protected] ~]$ cat /tmp/save-both
find: ‘/etc/lvm/archive’: Permission denied
find: ‘/etc/lvm/backup’: Permission denied
find: ‘/etc/lvm/cache’: Permission denied
find: ‘/etc/pki/rsyslog’: Permission denied
find: ‘/etc/pki/pesign’: Permission denied
find: ‘/etc/ssh/sshd_config.d’: Permission denied
/etc/pam.d/passwd
find: ‘/etc/grub.d’: Permission denied
/etc/passwd
find: ‘/etc/sssd’: Permission denied
find: ‘/etc/polkit-1/rules.d’: Permission denied
find: ‘/etc/polkit-1/localauthority’: Permission denied
find: ‘/etc/cups/ssl’: Permission denied
find: ‘/etc/cni/net.d’: Permission denied
find: ‘/etc/nftables’: Permission denied
find: ‘/etc/audit’: Permission denied
find: ‘/etc/sudoers.d’: Permission denied
find: ‘/etc/libvirt’: Permission denied
find: ‘/etc/containers/networks’: Permission denied
find: ‘/etc/sos/cleaner’: Permission denied
find: ‘/etc/ipsec.d’: Permission denied
find: ‘/etc/firewalld’: Permission denied
10、The output and the generated error attached to an existing file.
[[email protected] ~]$ find /etc -name passwd >> /tmp/save-both 2>&1
[[email protected] ~]$ cat /tmp/save-both
find: ‘/etc/lvm/archive’: Permission denied
find: ‘/etc/lvm/backup’: Permission denied
find: ‘/etc/lvm/cache’: Permission denied
find: ‘/etc/pki/rsyslog’: Permission denied
find: ‘/etc/pki/pesign’: Permission denied
find: ‘/etc/ssh/sshd_config.d’: Permission denied
/etc/pam.d/passwd
find: ‘/etc/grub.d’: Permission denied
/etc/passwd
find: ‘/etc/sssd’: Permission denied
find: ‘/etc/polkit-1/rules.d’: Permission denied
find: ‘/etc/polkit-1/localauthority’: Permission denied
find: ‘/etc/cups/ssl’: Permission denied
find: ‘/etc/cni/net.d’: Permission denied
find: ‘/etc/nftables’: Permission denied
find: ‘/etc/audit’: Permission denied
find: ‘/etc/sudoers.d’: Permission denied
find: ‘/etc/libvirt’: Permission denied
find: ‘/etc/containers/networks’: Permission denied
find: ‘/etc/sos/cleaner’: Permission denied
find: ‘/etc/ipsec.d’: Permission denied
find: ‘/etc/firewalld’: Permission denied
find: ‘/etc/lvm/archive’: Permission denied
find: ‘/etc/lvm/backup’: Permission denied
find: ‘/etc/lvm/cache’: Permission denied
find: ‘/etc/pki/rsyslog’: Permission denied
find: ‘/etc/pki/pesign’: Permission denied
find: ‘/etc/ssh/sshd_config.d’: Permission denied
/etc/pam.d/passwd
find: ‘/etc/grub.d’: Permission denied
/etc/passwd
find: ‘/etc/sssd’: Permission denied
find: ‘/etc/polkit-1/rules.d’: Permission denied
find: ‘/etc/polkit-1/localauthority’: Permission denied
find: ‘/etc/cups/ssl’: Permission denied
find: ‘/etc/cni/net.d’: Permission denied
find: ‘/etc/nftables’: Permission denied
find: ‘/etc/audit’: Permission denied
find: ‘/etc/sudoers.d’: Permission denied
find: ‘/etc/libvirt’: Permission denied
find: ‘/etc/containers/networks’: Permission denied
find: ‘/etc/sos/cleaner’: Permission denied
find: ‘/etc/ipsec.d’: Permission denied
find: ‘/etc/firewalld’: Permission denied
二、构建管道
说明:Pipeline is a sequence of one or more orders,用竖线字符“|”分隔;Pipeline will be the first command of the standard output connected to the next command's standard input.想象一下:Data is through the pipe flow from one process to another process,Every command slightly in its flow through the pipe to do some changes.
重定向:Will send file to standard output or standard input from a file.
管道:Will be a process of standard output is sent to another process standard input.
示例
1、使用lsThe output of the command and uselessOn the terminal display output in the form of a once.
[[email protected] ~]$ ls -l /usr/bin | less
2、lsThe output of the command towc -l,Statistics from the userlsReceive the number of lines,And the bank number displayed in the end.
[[email protected] ~]$ ls | wc -l
9
3、Input in the pipe out of the current query results before10行,And the final result is redirected to a file.
[[email protected] ~]$ ls -t | head -n 10 > /tmp/ten-last
[[email protected] ~]$ cat /tmp/ten-last
rhel-8.6-x86_64-dvd.iso
Downloads
Desktop
Documents
Music
Pictures
Public
Templates
Videos
三、管道、重定向和tee命令
When redirection and pipe combination,shellWill first set the whole pipeline,And then redirect input/输出.If in the middle of the pipe using the output redirection,The output will be transferred to file,Rather than heading to the pipeline of the next command.teeOrder to overcome this limitation;在管道中,teeCopies of its standard input to its standard output,And also to standard output is redirected to the designated command parameter file.Imagine if the data flowing through a pipe of water,那么可将teeVisual into pipesT形接头,Responsible for the flow of output in both directions.
示例
1、将ls命令的输出重定向到文件,And the output to aless,So that on the terminal display in the form of one screen at a time.
[[email protected] ~]$ ls -l | tee /tmp/save-both | less
2、If at the end of pipe to usetee,You can save the final output of the command and output to the terminal at the same time.
[[email protected] ~]$ ls -t | head -n 10 | tee /tmp/ten-lest
rhel-8.6-x86_64-dvd.iso
Downloads
Desktop
Documents
Music
Pictures
Public
Templates
Videos
[[email protected] ~]$ cat /tmp/ten-lest
rhel-8.6-x86_64-dvd.iso
Downloads
Desktop
Documents
Music
Pictures
Public
Templates
Videos
边栏推荐
- Cortex-A7 MPCore 架构
- The strongest distributed lock tool: Redisson
- Metasploit——辅助模块(Auxiliary)
- How to play with container local storage through open-local? | Dragon Lizard Technology
- openEuler 熊伟:如何看待开源社区中的 SIG 组织模式?
- 史上最全架构师知识图谱
- 一口气拿下6个大厂offer,是什么水平
- One-key login principle of local number
- 进行知识管理的好处有哪些?
- Detailed explanation of JVM memory model and structure (five model diagrams)
猜你喜欢
The senior told me that the MySQL of the big factory is connected through SSH
偷偷盘点一下各大互联网公司的实习薪资
What platform is EPIC?
读大学有用吗?
Discuz! Forum program installation + template configuration tutorial
Volatile:JVM 我警告你,我的人你别乱动
动态RDLC报表(七)
What are some good open source automation testing frameworks to recommend?
100+开箱即用的AI工具箱;程序员150岁长寿指南;『地理空间数据科学』课程资料;Graphic数据可视化图表库;前沿论文 | ShowMeAI资讯日报
Cortex-A7 MPCore 架构
随机推荐
win10 uwp 模拟网页输入
[SUCTF 2019]CheckIn
leetcode/链表中环的入口节点
神秘的程序员(20-30)
win10 uwp 获取指定的文件 AQS
动态RDLC报表(七)
国能准能集团研发矿山数字孪生系统 填补国内采矿行业空白
方舟开服务器Vmware虚拟机安装不上?
郭炜(郭大侠):九个关于开源的 Yes or No
2022 全球 AI 模型周报
mysql如何查看所有复合主键的表名?
Axure实现表格带滚动条
openEuler Xiong Wei: How do you view the SIG organization model in the open source community?
loadrunner脚本--参数化
论文解读:Deep-4MCW2V:基于序列的预测指标,以鉴定大肠杆菌中的N4-甲基环胞嘧啶位点
win10 uwp 自定义控件 SplitViewItem
毕昇编译器优化:Lazy Code Motion
eyb:Redis学习(3)
Apache Doris 社区 PMC 杨政国:开源项目如何在自身和社区的需求中取得平衡?
国际土壤模型协会 International Soil Modeling Consortium-ISMC