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

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

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

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

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

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

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

  [[email protected] ~]# echo Welcome 1>nsw.txt 
  [[email protected] ~]# cat nsw.txt 
  Welcome

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

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

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

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

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

   [[email protected] ~]# echo My name is nsw1>>nsw.txt 
   [[email protected] ~]# cat nsw.txt 
   Welcome
   My name is nsw
   My name is nsw1

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

   [[email protected] ~]# echo My name is nsw 1>>nsw.txt 
   [[email protected] ~]# cat nsw.txt 
   Welcome
   My name is nsw
   My name is nsw1
   My name is nsw

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

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

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

3.标准错误重定向2>

   [[email protected] ~]# cat hello.txt 
   Hello
   [[email protected] ~]# eho test
   -bash: eho: command not found
   [[email protected] ~]# eho test 2>hello.txt 
   [[email protected] ~]# cat hello.txt 
   -bash: eho: command not found

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

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

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

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

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

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

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

5.标准输入重定向:<

   [[email protected] ~]# echo 1 2 3 4 5 6 >test.txt
   [[email protected] ~]# cat test.txt 
   1 2 3 4 5 6
   [[email protected] ~]# xargs -n2 test.txt 
   ^C
   [[email protected] ~]# xargs -n2 <test.txt 
   1 2
   3 4
   5 6

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

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

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

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

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

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

相关文章:

  • 2021-06-14
  • 2021-06-28
  • 2022-03-07
猜你喜欢
  • 2021-04-02
  • 2021-06-01
  • 2021-11-13
相关资源
相似解决方案