暂无图片
暂无图片
1
暂无图片
暂无图片
暂无图片

Linux中的双向重定向tee命令

watson 2025-03-16
18

【导读】


    tee命令会将数据流分送到文件与屏幕(stdout)。


一 tee参数介绍

[root@achao achao]# tee --help Usage: tee [OPTION]... [FILE]...Copy standard input to each FILE, and also to standard output.
-a, --append append to the given FILEs, do not overwrite -i, --ignore-interrupts ignore interrupt signals --help display this help and exit --version output version information and exit
If a FILE is -, copy again to standard output.
复制

    

二  参数-a以累加的形式追加到文件中

[root@achao achao]# rm -rf * [root@achao achao]# lltotal 0[root@achao achao]# head -3 /etc/passwd | tee demo.txtroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologin[root@achao achao]# cat demo.txt root:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologin
复制

    发现在屏幕上显示的同时并且生成了一个新文件deno.txt,将stdout的数据记录。

[root@achao achao]# ls /bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var[root@achao achao]# ls / | tee demo.txtbinbootdev......[root@achao achao]# cat demo.txt binbootdev......
复制

    发现以前记录的信息不见了呢!这个时候就需要加参数-a。

[root@achao achao]# head -3 /etc/passwd | tee demo.txtroot:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologin[root@achao achao]# cat demo.txt root:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologin[root@achao achao]# head -3 /etc/group | tee -a demo.txt root:x:0:bin:x:1:daemon:x:2:[root@achao achao]# cat demo.txt root:x:0:0:root:/root:/bin/bashbin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinroot:x:0:bin:x:1:daemon:x:2:[root@achao achao]# 
复制

    这样就以累加的形式记录下来了。在具体的生产环境中还是很重要的。有想法的同学们一定想起了重定向的功能的。这里简单回忆一下。


三 重定向的使用


   标准输入(stdin):代码为0,使用<或<<;

   标准输出(stdout):代码为1,使用>或>>;

   标准错误输出(stderr):代码为2,使用2>或2>>;

   1> : 以覆盖的方式将【正确的数据】输出到指定的文件或设备上;

   1>> : 以累加的方式将【正确的数据】输出到指定的文件或设备上;

   2> : 以覆盖的方式将【错误的数据】输出到指定的文件或设备上;

   2>> : 已累加的方式将【错误的数据】输出到指定的文件或设备上;


    更加详细的知识可以点击下发链接

    Linux的数据流重定向2>&1/&>/2>>/&>/<<讲解

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论