Linux----tee命令详细使用方法

【原文链接】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是将文件清空后再写入。

[root@jiayi-centos-01 opt]# echo "hello world" | tee demo.txt
hello world
[root@jiayi-centos-01 opt]# cat demo.txt
hello world
[root@jiayi-centos-01 opt]# echo "hello world" | tee demo.txt
hello world
[root@jiayi-centos-01 opt]# cat demo.txt
hello world
[root@jiayi-centos-01 opt]#

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

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

[root@jiayi-centos-01 opt]# echo "hello world" | tee -a demo.txt
hello world
[root@jiayi-centos-01 opt]# cat demo.txt
hello world
[root@jiayi-centos-01 opt]# echo "hello world" | tee -a demo.txt
hello world
[root@jiayi-centos-01 opt]# cat demo.txt
hello world
hello world
[root@jiayi-centos-01 opt]#

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

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

[root@jiayi-centos-01 opt]# cat /etc/ssh/ssh_config
#       $OpenBSD: ssh_config,v 1.30 2016/02/20 23:06:23 sobrado Exp $

# This is the ssh client system-wide configuration file.  See
# ssh_config(5) for more information.  This file provides defaults for
# users, and the values can be changed in per-user configuration files
# or on the command line.

# Configuration data is parsed as follows:
#  1. command line options
#  2. user-specific file
#  3. system-wide file
# Any configuration value is only changed the first time it is set.
# Thus, host-specific definitions should be at the beginning of the
# configuration file, and defaults at the end.

# Site-wide defaults for some commonly used options.  For a comprehensive
# list of available options, their meanings and defaults, please see the
# ssh_config(5) man page.

# Host *
#   ForwardAgent no
#   ForwardX11 no
#   RhostsRSAAuthentication no
#   RSAAuthentication yes
#   PasswordAuthentication yes
#   HostbasedAuthentication no
#   GSSAPIAuthentication no
#   GSSAPIDelegateCredentials no
#   GSSAPIKeyExchange no
#   GSSAPITrustDNS no
#   BatchMode no
#   CheckHostIP yes
#   AddressFamily any
#   ConnectTimeout 0
#   StrictHostKeyChecking ask
#   IdentityFile ~/.ssh/identity
#   IdentityFile ~/.ssh/id_rsa
#   IdentityFile ~/.ssh/id_dsa
#   IdentityFile ~/.ssh/id_ecdsa
#   IdentityFile ~/.ssh/id_ed25519
#   Port 22
#   Protocol 2
#   Cipher 3des
#   Ciphers aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,aes128-cbc,3des-cbc
#   MACs hmac-md5,hmac-sha1,umac-64@openssh.com,hmac-ripemd160
#   EscapeChar ~
#   Tunnel no
#   TunnelDevice any:any
#   PermitLocalCommand no
#   VisualHostKey no
#   ProxyCommand ssh -q -W %h:%p gateway.example.com
#   RekeyLimit 1G 1h
#
# Uncomment this if you want to use .local domain
# Host *.local
#   CheckHostIP no

Host *
        GSSAPIAuthentication yes
# If this option is set to yes then remote X11 clients will have full access
# to the original X11 display. As virtually no X11 client supports the untrusted
# mode correctly we set this to yes.
        ForwardX11Trusted yes
# Send locale-related environment variables
        SendEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
        SendEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
        SendEnv LC_IDENTIFICATION LC_ALL LANGUAGE
        SendEnv XMODIFIERS
[root@jiayi-centos-01 opt]#

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

[root@jiayi-centos-01 opt]# grep -v '^#' /etc/ssh/ssh_config | tee demo.txt





Host *
        GSSAPIAuthentication yes
        ForwardX11Trusted yes
        SendEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
        SendEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
        SendEnv LC_IDENTIFICATION LC_ALL LANGUAGE
        SendEnv XMODIFIERS
[root@jiayi-centos-01 opt]# cat demo.txt





Host *
        GSSAPIAuthentication yes
        ForwardX11Trusted yes
        SendEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
        SendEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
        SendEnv LC_IDENTIFICATION LC_ALL LANGUAGE
        SendEnv XMODIFIERS
[root@jiayi-centos-01 opt]#

  • 3
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

redrose2100

您的鼓励是我最大的创作动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值