Linux - 命令重定向
命令重定向, 就是将目前得到的数据转移到指定的地方.分为以下几种:
>
>>
1>
2>
1>>
2>>
<
1. > 与 >>
先看一个简单的例子. 如果执行ll指令, 会在屏幕上显示执行结果:
| [root@localhost yuechaotian]# pwd /home/yuechaotian [root@localhost yuechaotian]# ll 总用量 52 -r-------- 1 root root 22 9月 4 16:31 adsl帐号.txt -rw-rw-r-- 1 yuechaotian yuechaotian 2 12月 14 10:54 r.log -rw-rw-r-- 1 yuechaotian yuechaotian 0 12月 14 10:20 r.log.r -rw-rw-r-- 1 yuechaotian yuechaotian 24 12月 14 10:20 r.log.w drwx--x--x 3 root root 4096 12月 7 14:52 study drwxrwxrwx 2 yuechaotian yuechaotian 4096 12月 14 10:58 test -rw------- 1 root root 5789 12月 12 20:53 tnsnames.ora -r-------- 1 root root 22528 8月 31 10:26 房屋租赁合同.doc [root@localhost yuechaotian]# ll test/ 总用量 0 |
如果不想在屏幕上显示, 而是想把输出结果直接存储在指定的文件中, 可以使用 > 或 >>
| # > 将输出结果以"覆盖"的形式存储在指定的文件中, 若文件不存在则自动创建. [root@localhost yuechaotian]# ll > test/ll.log [root@localhost yuechaotian]# cat test/ll.log 总用量 52 -r-------- 1 root root 22 9月 4 16:31 adsl帐号.txt -rw-rw-r-- 1 yuechaotian yuechaotian 2 12月 14 10:54 r.log -rw-rw-r-- 1 yuechaotian yuechaotian 0 12月 14 10:20 r.log.r -rw-rw-r-- 1 yuechaotian yuechaotian 24 12月 14 10:20 r.log.w drwx--x--x 3 root root 4096 12月 7 14:52 study drwxrwxrwx 2 yuechaotian yuechaotian 4096 12月 14 11:01 test -rw------- 1 root root 5789 12月 12 20:53 tnsnames.ora -r-------- 1 root root 22528 8月 31 10:26 房屋租赁合同.doc # >> 将输出结果以“追加”的形式存储在指定的文件中, 若文件不存在则自动创建。 [root@localhost yuechaotian]# ll test/ >> test/ll.log [root@localhost yuechaotian]# cat test/ll.log 总用量 52 -r-------- 1 root root 22 9月 4 16:31 adsl帐号.txt -rw-rw-r-- 1 yuechaotian yuechaotian 2 12月 14 10:54 r.log -rw-rw-r-- 1 yuechaotian yuechaotian 0 12月 14 10:20 r.log.r -rw-rw-r-- 1 yuechaotian yuechaotian 24 12月 14 10:20 r.log.w drwx--x--x 3 root root 4096 12月 7 14:52 study drwxrwxrwx 2 yuechaotian yuechaotian 4096 12月 14 11:01 test -rw------- 1 root root 5789 12月 12 20:53 tnsnames.ora -r-------- 1 root root 22528 8月 31 10:26 房屋租赁合同.doc 总用量 4 -rw-r--r-- 1 root root 569 12月 14 11:01 ll.log |
那么如果指令执行失败呢? 输出结果就不会保存到指定文件中:
| [root@localhost yuechaotian]# ll testx > test/ll.log.2 ls: testx: 没有那个文件或目录 [root@localhost yuechaotian]# ll test/ 总用量 4 -rw-r--r-- 1 root root 631 12月 14 11:03 ll.log -rw-r--r-- 1 root root 0 12月 14 11:08 ll.log.2 [root@localhost yuechaotian]# cat test/ll.log.2 [root@localhost yuechaotian]# |
2. 1> 2>
如果需要将输出的正确结果保存到一个文件中, 输出的错误结果保存到另一个文件中,可以借助 1> 和 2>
| [root@localhost yuechaotian]# ll testx/ 1> test/ll.right 2> test/ll.wrong [root@localhost yuechaotian]# ll test/ 总用量 8 -rw-r--r-- 1 root root 631 12月 14 11:03 ll.log -rw-r--r-- 1 root root 0 12月 14 11:08 ll.log.2 -rw-r--r-- 1 root root 0 12月 14 11:11 ll.right -rw-r--r-- 1 root root 40 12月 14 11:11 ll.wrong [root@localhost yuechaotian]# cat test/ll.right [root@localhost yuechaotian]# cat test/ll.wrong ls: testx/: 没有那个文件或目录 |
Linux是通过什么来判断的呢?因为每个当前指令的执行结果都保存在环境变量“?”中,当指令执行成功时 ?=0,当指令执行失败时 ?=1。Linux就是通过它来判断输出结果保存到哪个文件中的:
| [root@localhost yuechaotian]# ll test/ 总用量 8 -rw-r--r-- 1 root root 631 12月 14 11:03 ll.log -rw-r--r-- 1 root root 0 12月 14 11:08 ll.log.2 -rw-r--r-- 1 root root 0 12月 14 11:11 ll.right -rw-r--r-- 1 root root 40 12月 14 11:11 ll.wrong [root@localhost yuechaotian]# echo $? 0 [root@localhost yuechaotian]# ll testx/ ls: testx/: 没有那个文件或目录 [root@localhost yuechaotian]# echo $? 1 |
如果想不论指令执行正确与否,都将结果输出到同一个指定文件中,需要借助 1> 和 2>&1:
| [root@localhost yuechaotian]# ll testx/ 1> test/ll.all 2>&1 [root@localhost yuechaotian]# cat test/ll.all ls: testx/: 没有那个文件或目录 |
如果可以提前预知错误的结果,只想保存正确的输出结果,而删除掉错误的结果,有什么好办法么?可以将错误的输出直接保存到“垃圾筒”中,这就借助到一个设备 /dev/null
| [root@localhost yuechaotian]#ll testx/ 1> test/ll.right.only 2>/dev/null |
3. 1>> 2>>
同样地,如果想将正确的输出结果追加到一个文件中,错误的输出结果追加到另一个文件中,就需要借助 1>> 和 2>> 了。它们的使用方法跟 1> 2> 类似,所不同的就是执行结果是追加到指定文件中,而不是覆盖。
| [root@localhost test]# cat ll.wrong ls: testx/: 没有那个文件或目录 [root@localhost test]# cat ll.right [root@localhost test]# cd subtest 1>> ll.right 2>> ll.wrong [root@localhost test]# cat ll.right [root@localhost test]# cat ll.wrong ls: testx/: 没有那个文件或目录 -bash: cd: subtest: 没有那个文件或目录 [root@localhost test]# ll ../ 1>> ll.right 2>> ll.wrong [root@localhost test]# cat ll.right 总用量 60 -r-------- 1 root root 22 9月 4 16:31 adsl帐号.txt -rw-r--r-- 1 root root 62 12月 14 11:02 ll.log -rw-rw-r-- 1 yuechaotian yuechaotian 2 12月 14 10:54 r.log -rw-rw-r-- 1 yuechaotian yuechaotian 0 12月 14 10:20 r.log.r -rw-rw-r-- 1 yuechaotian yuechaotian 24 12月 14 10:20 r.log.w -rw-r--r-- 1 root root 6 12月 14 11:06 s.log drwx--x--x 3 root root 4096 12月 7 14:52 study drwxrwxrwx 2 yuechaotian yuechaotian 4096 12月 14 11:20 test -rw------- 1 root root 5789 12月 12 20:53 tnsnames.ora -r-------- 1 root root 22528 8月 31 10:26 房屋租赁合同.doc [root@localhost test]# cat ll.wrong ls: testx/: 没有那个文件或目录 -bash: cd: subtest: 没有那个文件或目录 |
4. 1> 2>> 与 1>> 2>
如果想将正确的输出和错误的输出都“追加”到同一个文件中,怎么办?你想试试 1>> 和 2>>&1 吗:
| [root@localhost test]# rm * [root@localhost test]# echo "right and wrong" 1>> ll.r 2>>&1 -bash: syntax error near unexpected token `&' [root@localhost test]# ll 总用量 0 |
是的,没有 2>>&1 这个语法,但有 2>&1 这个语法,把 1>> 和 2>&1 接合起来看呢:
| [root@localhost test]# echo "right and wrong" 1>> ll.r 2>&1 [root@localhost test]# ll 总用量 4 -rw-r--r-- 1 root root 16 12月 14 11:39 ll.r [root@localhost test]# cat ll.r right and wrong [root@localhost test]# echo "right and wrong2" 1>> ll.r 2>&1 [root@localhost test]# cat ll.r right and wrong right and wrong2 [root@localhost test]# ll "right and wrong2" 1>> ll.r 2>&1 [root@localhost test]# cat ll.r right and wrong right and wrong2 ls: right and wrong2: 没有那个文件或目录 |
我们看,使用 1>> 和 2>&1 达到了目的。那就是说 1> 和 2>,1>> 和 2>> 并不是配对的关系,他们之间应该是可以交叉使用的。下面测试一下:
| [root@localhost test]# pwd /home/yuechaotian/test [root@localhost test]# rm * # 1. 输出正确则“追加”,输出错误则“覆盖” [root@localhost test]# echo "right_append & wrong_cover" 1>> ll.r 2> ll.w [root@localhost test]# cat ll.r right_append & wrong_cover [root@localhost test]# cat ll.w [root@localhost test]# echo "right_append & wrong_cover2" 1>> ll.r 2> ll.w [root@localhost test]# cat ll.r right_append & wrong_cover right_append & wrong_cover2 [root@localhost test]# cat ll.w [root@localhost test]# cd "right_append & wrong_cover2" 1>> ll.r 2> ll.w [root@localhost test]# cat ll.r right_append & wrong_cover right_append & wrong_cover2 [root@localhost test]# cat ll.w -bash: cd: right_append & wrong_cover2: 没有那个文件或目录 [root@localhost test]# cd "right_append & wrong_cover" 1>> ll.r 2> ll.w [root@localhost test]# cat ll.r right_append & wrong_cover right_append & wrong_cover2 [root@localhost test]# cat ll.w -bash: cd: right_append & wrong_cover: 没有那个文件或目录 #2. 输出正确则“覆盖”,输出错误则“追加” [root@localhost test]# echo "right_cover & wrong_append" 1> ll.r 2>> ll.w [root@localhost test]# cat ll.r right_cover & wrong_append [root@localhost test]# cat ll.w -bash: cd: right_append & wrong_cover: 没有那个文件或目录 [root@localhost test]# cd "right_cover & wrong_append" 1> ll.r 2>> ll.w [root@localhost test]# cat ll.r [root@localhost test]# cat ll.w -bash: cd: right_append & wrong_cover: 没有那个文件或目录 -bash: cd: right_cover & wrong_append: 没有那个文件或目录 |
一个小问题:如果输出正确则“追加”,输出错误则直接删除。怎么实现?
现在有两种方法了:1>>
2>> /dev/null 和 1>>
2>/dev/null。其实“追加”到垃圾筒与“覆盖”到垃圾筒效果是一样的。设备/dev/null就象一个无底洞,扔进去的东西瞬间就消失了。同样地,也可以将错误的输出保存起来,而将正确的输出“扔”到垃圾筒中。
5. <
< 的作用,就是将原本应该由键盘输入的数据经由文件读入。比如发送邮件,可以使用键盘输入邮件内容,也可以使用 < 将保存在磁盘中的文件读出,并发送出去
| # 1. 使用键盘输入方式来发送邮件给yuechaotian [root@localhost test]# mail -s "hi, yuechaotian" yuechaotian Hi, I'm root. . Cc: [root@localhost test]# su - yuechaotian [yuechaotian@localhost ~]$ [yuechaotian@localhost ~]$ procmail -v procmail v3.22 2001/09/10 Copyright (c) 1990-2001, Stephen R. van den Berg Copyright (c) 1997-2001, Philip A. Guenther Submit questions/answers to the procmail-related mailinglist by sending to: And of course, subscription and information requests for this list to: Locking strategies: dotlocking, fcntl() Hi, I'm root. Hi, I'm root. From root@localhost.localdomain Sun Dec 14 12:33:12 2008 Hi, I'm root, I send this mail by using <. [yuechaotian@localhost ~]$ |
6. 何时使用命令重定向
*当屏幕输出的信息很重要,我们需要将它保存起来时;
*背景执行中的程序,不希望它干扰屏幕正常的输出结果时;
*一些系统的例行性命令(比如写在/etc/crontab中的文件)的执行结果,希望它可以保存下来时;
*一些执行命令,我们已经知道可能的错误信息,所以想以 2> /dev/null 将它丢掉时;
*错误信息与正确欣喜需要分别输出时;
*其它需要使用命令重定向的情况时。
Linux - 命令重定向的更多相关文章
- linux命令重定向>、>>、 1>、 2>、 1>>、 2>>、 <
重定向命令其实用得不少吧,只是重来都没有仔细看过,这波正好又用到 又有空总结一波. 先看>和>>: 他们俩其实唯一的区别就是>是重定向到一个文件,>>是追加内容到文 ...
- linux命令重定向>、>>、 1>、 2>、 1>>、 2>>、 <(转)
原文章地址:https://www.cnblogs.com/piperck/p/6219330.html >和>>: 他们俩其实唯一的区别就是>是重定向到一个文件,>&g ...
- LINUX常用命令--重定向、管道篇(四)
一.Linux重定向 重定向能够实现Linux命令的输入输出与文件之间重定向,以及实现将多个命令组合起来实现更加强大的命令.这部分涉及到的比较多的命令主要有: 涉及到的比较多的命令主要有: cat:连 ...
- Linux输入输出重定向和文件查找值grep命令
Linux输入输出重定向和文件查找值grep命令 一.文件描述符Linux 的shell命令,可以通过文件描述符来引用一些文件,通常使用到的文件描述符为0,1,2.Linux系统实际上有12个文件描述 ...
- linux 命令 htop & 重定向 top, bashrc文件
最近在用linux服务器跑程序,有几条linux命令还蛮重要的,总结一下: 1. 直接跑代码: python test.py 2. 若想程序在后台跑,即使本地和服务器断开也能运行: nohup pyt ...
- Linux命令之文件重定向2
linux中重定向用符号“>”表示,语法一般是 源文件 > 目标文件 1)创出.txt文件touch 1.txt 注意:创建文件夹用mkdir 2)向.txt文件中写入内容 注意:①cat ...
- Linux命令- echo、grep 、重定向、1>&2、2>&1的介绍
最近笔试遇到一道题,关于Linux命令的,题目如下 下面两条命令分别会有怎样的输出 echo hello 1>&2 |grep aaa echo hello 2>&1 ...
- Linux入门之常用命令(6)Bash命令重定向 管线命令
命令重定向 将目前所得数据转移到其他地方 > 将输出结果导入文件 如 ls -l / >test (1)若test文件不存在则创建 (2)若test文件存在 清空后写入 > ...
- Linux实战教学笔记04:Linux命令基础
第四节:Linux命令基础 标签(空格分隔):Linux实战教学笔记 第1章 认识操作环境 root:当前登陆的用户名 @分隔符 chensiqi:主机名 -:当前路径位置 用户的提示符 1.1 Li ...
随机推荐
- MVC5+Easyui1.3.6+EF6 开发部分备忘笔记
一点一点增加,后面继续. 1.Row Editing in DataGrid 编辑,总是绑定不了checkbox的问题
- try except else
try except 语句还有一个可选的else子句,如果使用这个子句,那么必须放在所有的except子句之后.这个子句将在try子句没有发生任何异常的时候执行.例如: for arg in sys. ...
- suiyi
<?php namespace app\controllers; use Yii;use app\models\Device;use app\models\DeviceSearch;use ap ...
- Python实现Json结构对比的小工具兼谈编程求解问题
摘要: 通过使用Python编写一个解析Json结构对比的小工具,来提炼编程求解的通用步骤和技巧. 难度: 初级 先上代码. jsondiff.py #!/usr/bin/python #_*_enc ...
- MySQL重装失败,could not start the service MySQL.Error:0
MySQL5.5 安装失败现象: mysqld.exe [6132] 中发生了未经处理的 win32 异常 could not start the service MySQL.Error:0 1.在 ...
- Java性能优化——HashCode的使用
背景 告警子系统监控4万个大网元所有端口的某些指标数据,根据阈值配置判断是否产生告警.采集——数据处理子系统每5分钟会主动采集24万次数据,发送24万条消息给告警子系统,这24万条消息涉及100万实体 ...
- python 多进程并发接口测试实例
#encoding=utf-8 import requests import json import os import hashlib print "register------" ...
- 2018-2019-2 20165209 《网络对抗技术》Exp6:信息搜集与漏洞扫描
2018-2019-2 20165209 <网络对抗技术>Exp6:信息搜集与漏洞扫描 1 基础问题回答和实验内容 1.1基础问题回答 (1)哪些组织负责DNS,IP的管理. Intern ...
- P4281 [AHOI2008]紧急集合 / 聚会
P4281 [AHOI2008]紧急集合 / 聚会 lca 题意:求3个点的lca,以及3个点与lca的距离之和. 性质:设点q1,q2,q3 两点之间的lca t1=lca(q1,q2) t2=lc ...
- 【资源】分享一个最新版sublime 3143的注册码,亲测可用
注:请勿用作商业用途,有能力者请购买正版!!! —– BEGIN LICENSE —– TwitterInc 200 User License EA7E-890007 1D77F72E 390CDD9 ...