Linux 重定向

一、重定向符号:> >> 1> 1>> 2> 2>> < <<

1.标准输出重定向: >与1>
1.1 >

  [root@NSW ~]# ls
  anaconda-ks.cfg  install.log.syslog
  [root@NSW ~]# echo My name is NSW>nsw.txt      
  [root@NSW ~]# cat nsw.txt 
  My name is NSW

//新建nsw.txt,并将>左边的字符写入到文件中。目录下无文件,自动创建文件。

1.2 1>
[root@NSW ~]# echo My name is NSW 1>test.txt
[root@NSW ~]# cat test.txt
My name is NSW
//同理。

[root@NSW ~]# cat nsw.txt 
  My name is NSW
  [root@NSW ~]# echo I love oldboy >nsw.txt 
  [root@NSW ~]# cat nsw.txt 
  I love oldboy

//可以看出>会将源文件的内容覆盖。(谨慎)

  [root@NSW ~]# echo Welcome 1>nsw.txt 
  [root@NSW ~]# cat nsw.txt 
  Welcome

//1>同样也会先覆盖原文件的内容。

小结:重定向符>和1>都为输出重定向;
被输入的文件有则直接输入,无则自动创建该文件并输入;
将符号左边的字符串输入到右边文件中且覆盖原文件。

2.追加输出重定向:>> 1>>

   [root@NSW ~]# ls
   anaconda-ks.cfg  install.log.syslog  nsw.txt  test.txt
   [root@NSW ~]# cat nsw.txt 
   Welcome
   [root@NSW ~]# echo My name is nsw>>nsw.txt 
   [root@NSW ~]# cat nsw.txt 
   Welcome
   My name is nsw

//可以看出确实在云文件最后面新追加了My name is nsw没有覆盖源文件。

   [root@NSW ~]# echo My name is nsw1>>nsw.txt 
   [root@NSW ~]# cat nsw.txt 
   Welcome
   My name is nsw
   My name is nsw1

//这需要注意的是符号左面需要空一格要不系统会认为1是输入的字符。

   [root@NSW ~]# echo My name is nsw 1>>nsw.txt 
   [root@NSW ~]# cat nsw.txt 
   Welcome
   My name is nsw
   My name is nsw1
   My name is nsw

//同理1>>和>>作用相同。

   [root@NSW ~]# ls
   anaconda-ks.cfg  install.log.syslog  nsw.txt  test.txt
   [root@NSW ~]# echo Hello >>hello.txt
   [root@NSW ~]# cat hello.txt 
   Hello
   [root@NSW ~]# cat HELLO.txt 
   HELLO

小结:追加输出重定向>>和1>>,将内容追加到指定文件中最后一行的下一行,不会覆盖源文件;
指定目录下有源文件直接追加,没有则自定创建文件并追加内容。

3.标准错误重定向2>

   [root@NSW ~]# cat hello.txt 
   Hello
   [root@NSW ~]# eho test
   -bash: eho: command not found
   [root@NSW ~]# eho test 2>hello.txt 
   [root@NSW ~]# cat hello.txt 
   -bash: eho: command not found

//将错误重定向到指定文件中并覆盖原文件内容。

4.错误追加重定向2>>

    [root@NSW ~]# cat test.txt 
    My name is NSW
    [root@NSW ~]# eho Hello 2>> test.txt 
    [root@NSW ~]# cat test.txt 
    My name is NSW
   -bash: eho: command not found

//将错误追加到指定文件中。

    [root@NSW ~]# ls
    anaconda-ks.cfg  install.log.syslog
    [root@NSW ~]# eho hello 2>test.txt
    [root@NSW ~]# cat test.txt 
    -bash: eho: command not found
    [root@NSW ~]# 
    [root@NSW ~]# 
    [root@NSW ~]# eho hello 2>>test.txt
    [root@NSW ~]# cat test.txt 
   -bash: eho: command not found
   -bash: eho: command not found

//无论2>还是2>>在目录下没有指定文件时都会自动创建文件。

小结:标准错误输出重定向和错误追加重定向都是将执行错误的提示内容放入指定文件中,故障排查,区别在于一个是覆盖一个是追加。

5.标准输入重定向:<

   [root@NSW ~]# echo 1 2 3 4 5 6 >test.txt
   [root@NSW ~]# cat test.txt 
   1 2 3 4 5 6
   [root@NSW ~]# xargs -n2 test.txt 
   ^C
   [root@NSW ~]# xargs -n2 

//用来将<右侧文件中的内容输出给左侧的命令,可以使用<符号的命令很少,xargs为其中之一。

6.追加输入重定向:<<

   [root@NSW ~]# ls
   anaconda-ks.cfg  install.log.syslog  test.txt
   [root@NSW ~]# cat >>nsw.txt< Hello!
   > My name is NSW
   > EOF
   [root@NSW ~]# cat nsw.txt 
   Hello!
   My name is NSW

//将多行内容追加输入到指定文件中,指定目录下没有文件则新建。

小结:<是用来将符号右侧文本的内容作为左侧命令的执行条件;
<<是方便输入多行内容到指定文件中。

============================================================
如果文中有错误的地方欢迎大家指出,谢谢!
Linux 重定向_第1张图片

你可能感兴趣的:(Linux基础命令)