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 ...
随机推荐
- lambda函数和map函数
lambda函数,简化了函数定义的书写形式,使代码更为简洁,但是使用自定义函数的定义方式更为直观,易理解 g = lambda x:x+1 #上面的lambda表达式相当于下面的自定义函数 def g ...
- XDU1024简单逆序对(贪心||分治)
题目描述 逆序对问题对于大家来说已经是非常熟悉的问题了,就是求i<j时,a[i] > a[j]的组数.现在请你求出一串数字中的逆序对的个数,需要注意的是,这些数字均在[0,9]之内. 输入 ...
- 文件操作(CRT、C++、WIN API、MFC)
一.使用CRT函数文件操作 二.使用标准C++库 std::fstream std::string 1)std::string对象内部存储了一个C的字符串,以'\0'结尾的. 2)std::strin ...
- VS2010/MFC编程入门之三十九(文档、视图和框架:概述)
前面几节讲了菜单.工具栏和状态栏的使用,鸡啄米本节开始将为大家讲解文档.视图和框架的知识. 文档.视图和框架简介 在VS2010/MFC编程入门之三十四(菜单:VS2010菜单资源详解)创建的单文档工 ...
- IO(File)
1. 一个File类的对象,表示了磁盘上的文件或目录 2. File类提供了与平台无关的方法来对磁盘上的文件或目录进行操作 3. File对象可用来获取或处理与磁盘文件相关的信息,如:权限,时间,日期 ...
- Linux基础命令---head
head 显示文件开头的几行,默认显示10行,可以使用选项-n来指定行数.此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.SUSE.openSUSE.Fedora. 1.语法 ...
- 5+App使用定位
1.定位方法 5+App定位方法:5+ API中的Geolocation模块 Geolocation目前支持h5内置的定位,百度,高德.h5内置定位支持wgs84坐标系:百度支持gcj ...
- web前端----jQuery基础语法
一.jQuery基础1.为什么要用jquery? 写起来简单,省事,开发效率高,兼容性好2.什么是jQuery? jQuery是一个兼容多浏览器的JavaScript库(类似python里面的模块)3 ...
- 【读书笔记】SpringBoot读书笔记
整体目录结构: 一.入门 二.开发第一个应用程序 三.自定义配置 四.测试 五.Groovy与Spring Boot Cli 六.在Spring Boot中使用Grails 七.深入Actuator ...
- UVA12995 Farey Sequence
UVA12995 Farey Sequence 欧拉函数 同仪仗队那题几乎相同,本质都是求欧拉函数的和 #include<cstdio> #define N 1000000 ],i,j,t ...