Linux----tee命令详细使用方法
作者:redrose2100   类别:    日期:2022-12-15 12:54:38    阅读:948 次   消耗积分:0 分

【原文链接】Linux——tee命令详细使用方法

一、tee命令使用方法

1.1 tee命令的功能

tee命令主要作用就是将标准出中的内容在控制台显示的同时并写入文件,如果直接使用重定向符,则只会写入文件,而不会在控制台显示,tee就是为了解决这个问题的。

1.2 tee命令的选项参数

  • -a: 通过追加的方式将内容写入文件

二、tee命令使用实例

2.1 将标准输出的内容同时向控制台和文件中写入,同时写文件时将文件内容清空后写入

如下,通过echo打印hello world字符串,同时将hello world字符串写入demo.txt文件,执行两遍后,demo.txt中仍然是hello world字符串,因此此时tee是将文件清空后再写入。

  1. [root@jiayi-centos-01 opt]# echo "hello world" | tee demo.txt
  2. hello world
  3. [root@jiayi-centos-01 opt]# cat demo.txt
  4. hello world
  5. [root@jiayi-centos-01 opt]# echo "hello world" | tee demo.txt
  6. hello world
  7. [root@jiayi-centos-01 opt]# cat demo.txt
  8. hello world
  9. [root@jiayi-centos-01 opt]#

2.2 将标准输出的内容同时向控制台和文件中写入,同时写文件时在文件后追加

如下,通过-a参数即可做到。

  1. [root@jiayi-centos-01 opt]# echo "hello world" | tee -a demo.txt
  2. hello world
  3. [root@jiayi-centos-01 opt]# cat demo.txt
  4. hello world
  5. [root@jiayi-centos-01 opt]# echo "hello world" | tee -a demo.txt
  6. hello world
  7. [root@jiayi-centos-01 opt]# cat demo.txt
  8. hello world
  9. hello world
  10. [root@jiayi-centos-01 opt]#

2.3 典型应用:将配置文件内容导出到另外一个文件,并且去掉注释

如下,查看 /etc/ssh/ssh_config 配置文件如下

  1. [root@jiayi-centos-01 opt]# cat /etc/ssh/ssh_config
  2. # $OpenBSD: ssh_config,v 1.30 2016/02/20 23:06:23 sobrado Exp $
  3. # This is the ssh client system-wide configuration file. See
  4. # ssh_config(5) for more information. This file provides defaults for
  5. # users, and the values can be changed in per-user configuration files
  6. # or on the command line.
  7. # Configuration data is parsed as follows:
  8. # 1. command line options
  9. # 2. user-specific file
  10. # 3. system-wide file
  11. # Any configuration value is only changed the first time it is set.
  12. # Thus, host-specific definitions should be at the beginning of the
  13. # configuration file, and defaults at the end.
  14. # Site-wide defaults for some commonly used options. For a comprehensive
  15. # list of available options, their meanings and defaults, please see the
  16. # ssh_config(5) man page.
  17. # Host *
  18. # ForwardAgent no
  19. # ForwardX11 no
  20. # RhostsRSAAuthentication no
  21. # RSAAuthentication yes
  22. # PasswordAuthentication yes
  23. # HostbasedAuthentication no
  24. # GSSAPIAuthentication no
  25. # GSSAPIDelegateCredentials no
  26. # GSSAPIKeyExchange no
  27. # GSSAPITrustDNS no
  28. # BatchMode no
  29. # CheckHostIP yes
  30. # AddressFamily any
  31. # ConnectTimeout 0
  32. # StrictHostKeyChecking ask
  33. # IdentityFile ~/.ssh/identity
  34. # IdentityFile ~/.ssh/id_rsa
  35. # IdentityFile ~/.ssh/id_dsa
  36. # IdentityFile ~/.ssh/id_ecdsa
  37. # IdentityFile ~/.ssh/id_ed25519
  38. # Port 22
  39. # Protocol 2
  40. # Cipher 3des
  41. # Ciphers aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,aes128-cbc,3des-cbc
  42. # MACs hmac-md5,hmac-sha1,umac-64@openssh.com,hmac-ripemd160
  43. # EscapeChar ~
  44. # Tunnel no
  45. # TunnelDevice any:any
  46. # PermitLocalCommand no
  47. # VisualHostKey no
  48. # ProxyCommand ssh -q -W %h:%p gateway.example.com
  49. # RekeyLimit 1G 1h
  50. #
  51. # Uncomment this if you want to use .local domain
  52. # Host *.local
  53. # CheckHostIP no
  54. Host *
  55. GSSAPIAuthentication yes
  56. # If this option is set to yes then remote X11 clients will have full access
  57. # to the original X11 display. As virtually no X11 client supports the untrusted
  58. # mode correctly we set this to yes.
  59. ForwardX11Trusted yes
  60. # Send locale-related environment variables
  61. SendEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
  62. SendEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
  63. SendEnv LC_IDENTIFICATION LC_ALL LANGUAGE
  64. SendEnv XMODIFIERS
  65. [root@jiayi-centos-01 opt]#

这里面有许多的注释,现在想将里面有效的配置内容导出为文件,同时在控制台显示

  1. [root@jiayi-centos-01 opt]# grep -v '^#' /etc/ssh/ssh_config | tee demo.txt
  2. Host *
  3. GSSAPIAuthentication yes
  4. ForwardX11Trusted yes
  5. SendEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
  6. SendEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
  7. SendEnv LC_IDENTIFICATION LC_ALL LANGUAGE
  8. SendEnv XMODIFIERS
  9. [root@jiayi-centos-01 opt]# cat demo.txt
  10. Host *
  11. GSSAPIAuthentication yes
  12. ForwardX11Trusted yes
  13. SendEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
  14. SendEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
  15. SendEnv LC_IDENTIFICATION LC_ALL LANGUAGE
  16. SendEnv XMODIFIERS
  17. [root@jiayi-centos-01 opt]#
始终坚持开源开放共享精神,同时感谢您的充电鼓励和支持!
版权所有,转载本站文章请注明出处:redrose2100, http://blog.redrose2100.com/article/507
个人成就
  • 博客总数: 613 
  • 阅读总量: 638747 
  • 2022年 : 371 篇 
  • 2023年 : 211 篇 
  • 2024年 : 31 篇 
  • 2025年 : 0 篇 
测试开发技术全栈公众号
DevOps技术交流微信群