【原文鏈接】Linux----tee命令詳細(xì)使用方法
一、tee命令使用方法
1.1 tee命令的功能
tee命令主要作用就是將標(biāo)準(zhǔn)出中的內(nèi)容在控制臺顯示的同時并寫入文件,如果直接使用重定向符,則只會寫入文件,而不會在控制臺顯示,tee就是為了解決這個問題的。
1.2 tee命令的選項參數(shù)
- -a: 通過追加的方式將內(nèi)容寫入文件
二、tee命令使用實例
2.1 將標(biāo)準(zhǔn)輸出的內(nèi)容同時向控制臺和文件中寫入,同時寫文件時將文件內(nèi)容清空后寫入
如下,通過echo打印hello world字符串,同時將hello world字符串寫入demo.txt文件,執(zhí)行兩遍后,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 將標(biāo)準(zhǔn)輸出的內(nèi)容同時向控制臺和文件中寫入,同時寫文件時在文件后追加
如下,通過-a參數(shù)即可做到。
[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 典型應(yīng)用:將配置文件內(nèi)容導(dǎo)出到另外一個文件,并且去掉注釋
如下,查看 /etc/ssh/ssh_config 配置文件如下文章來源:http://www.zghlxwxcb.cn/news/detail-480102.html
[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]#
這里面有許多的注釋,現(xiàn)在想將里面有效的配置內(nèi)容導(dǎo)出為文件,同時在控制臺顯示文章來源地址http://www.zghlxwxcb.cn/news/detail-480102.html
[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]#
到了這里,關(guān)于Linux----tee命令詳細(xì)使用方法的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!