Linux输入输出重定向

标准输入、标准输出、标准错误

  • STDIN,文件描述符:0;标准输入,默认从键盘读取信息;

  • STDOUT,文件描述符:1;标准输出,默认将输出结果输出至终端;

  • STDERR,文件描述符:2;标准错误,默认将输出结果输出至终端

常用重定向命令

将命令的标准输出重定向到文件中

SomeCommand > SomeFile.txt  

将命令的标准输出重定向、追加到文件中

SomeCommand >> SomeFile.txt

将命令的标准输出、标准错误重定向到文件中

SomeCommand &> SomeFile.txt  

将命令的标准输出、标准错误重定向、追加到文件中

SomeCommand &>> SomeFile.txt  

使用tee命令,将命令的标准输出、标准错误重定向到文件中

SomeCommand 2>&1 | tee SomeFile.txt

来自Stack Overflow的表格:(n.e.表示不存在)

          || visible in terminal ||   visible in file   || existing
  Syntax  ||  StdOut  |  StdErr  ||  StdOut  |  StdErr  ||   file   
==========++==========+==========++==========+==========++===========
    >     ||    no    |   yes    ||   yes    |    no    || overwrite
    >>    ||    no    |   yes    ||   yes    |    no    ||  append
          ||          |          ||          |          ||
   2>     ||   yes    |    no    ||    no    |   yes    || overwrite
   2>>    ||   yes    |    no    ||    no    |   yes    ||  append
          ||          |          ||          |          ||
   &>     ||    no    |    no    ||   yes    |   yes    || overwrite
   &>>    ||    no    |    no    ||   yes    |   yes    ||  append
          ||          |          ||          |          ||
 | tee    ||   yes    |   yes    ||   yes    |    no    || overwrite
 | tee -a ||   yes    |   yes    ||   yes    |    no    ||  append
          ||          |          ||          |          ||
 n.e. (*) ||   yes    |   yes    ||    no    |   yes    || overwrite
 n.e. (*) ||   yes    |   yes    ||    no    |   yes    ||  append
          ||          |          ||          |          ||
|& tee    ||   yes    |   yes    ||   yes    |   yes    || overwrite
|& tee -a ||   yes    |   yes    ||   yes    |   yes    ||  append
tee

  Read from standard input and write to standard output and files (or commands).
  More information: https://www.gnu.org/software/coreutils/tee.

  - Copy standard input to each file, and also to standard output:
    echo "example" | tee path/to/file

  - Append to the given files, do not overwrite:
    echo "example" | tee -a path/to/file

  - Print standard input to the terminal, and also pipe it into another program for further processing:
    echo "example" | tee /dev/tty | xargs printf "[%s]"

  - Create a directory called "example", count the number of characters in "example" and write "example" to the terminal:
    echo "example" | tee >(xargs mkdir) >(wc -c)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值