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()
Default rcfile:         $HOME/.procmailrc
        It may be writable by your primary group
Your system mailbox:    /var/mail/yuechaotian
[yuechaotian@localhost ~]$ cat /var/mail/yuechaotian
From [email protected]  Sun Dec 14 12:10:08 2008
Return-Path:
Received: from localhost.localdomain (localhost.localdomain [127.0.0.1])
        by localhost.localdomain (8.13.1/8.13.1) with ESMTP id mBE4A8U0001330
        for ; Sun, 14 Dec 2008 12:10:08 +0800Received: (from root@localhost)
        by localhost.localdomain (8.13.1/8.13.1/Submit) id mBE4A86X001329
        for yuechaotian; Sun, 14 Dec 2008 12:10:08 +0800
Date: Sun, 14 Dec 2008 12:10:08 +0800
From: root
Message-Id: <[email protected]>
To: [email protected]
Subject: hi, yuechaotian

Hi, I'm root.
# 2. 那好,现在我将邮件内容保存到 mail.txt 中,使用 < 将 mail.txt 中的内容重新发送给yuechaotian
[yuechaotian@localhost ~]$ su - root
Password:
[root@localhost ~]# cd /home/yuechaotian/
[root@localhost yuechaotian]# touch mail.txt
[root@localhost yuechaotian]# echo > mail.txt "Hi, I'm root, I send this mail by using <."
[root@localhost yuechaotian]# cat mail.txt
Hi, I'm root, I send this mail by using <.
[root@localhost yuechaotian]# mail -s "hi, yuechaotian" yuechaotian < mail.txt
[root@localhost yuechaotian]# su - yuechaotian
[yuechaotian@localhost ~]$ cat /var/mail/yuechaotian
From [email protected]  Sun Dec 14 12:10:08 2008
Return-Path:
Received: from localhost.localdomain (localhost.localdomain [127.0.0.1])
        by localhost.localdomain (8.13.1/8.13.1) with ESMTP id mBE4A8U0001330
        for ; Sun, 14 Dec 2008 12:10:08 +0800Received: (from root@localhost)
        by localhost.localdomain (8.13.1/8.13.1/Submit) id mBE4A86X001329
        for yuechaotian; Sun, 14 Dec 2008 12:10:08 +0800
Date: Sun, 14 Dec 2008 12:10:08 +0800
From: root
Message-Id: <[email protected]>
To: [email protected]
Subject: hi, yuechaotian

Hi, I'm root.

From [email protected]  Sun Dec 14 12:33:12 2008
Return-Path:
Received: from localhost.localdomain (localhost.localdomain [127.0.0.1])
        by localhost.localdomain (8.13.1/8.13.1) with ESMTP id mBE4XCun024530
        for ; Sun, 14 Dec 2008 12:33:12 +0800Received: (from root@localhost)
        by localhost.localdomain (8.13.1/8.13.1/Submit) id mBE4XChc024529
        for yuechaotian; Sun, 14 Dec 2008 12:33:12 +0800
Date: Sun, 14 Dec 2008 12:33:12 +0800
From: root
Message-Id: <[email protected]>
To: [email protected]
Subject: hi, yuechaotian

Hi, I'm root, I send this mail by using <.

[yuechaotian@localhost ~]$


6. 何时使用命令重定向

 *当屏幕输出的信息很重要,我们需要将它保存起来时;
 *背景执行中的程序,不希望它干扰屏幕正常的输出结果时;
 *一些系统的例行性命令(比如写在/etc/crontab中的文件)的执行结果,希望它可以保存下来时;
 *一些执行命令,我们已经知道可能的错误信息,所以想以 2> /dev/null 将它丢掉时;
 *错误信息与正确欣喜需要分别输出时;
 *其它需要使用命令重定向的情况时。

猜你喜欢

转载自www.cnblogs.com/keta/p/9888811.html