Support > About independent server > Basic usage of the tee command on Linux
Basic usage of the tee command on Linux
Time : 2025-02-15 13:41:35
Edit : Jtti

The Tee command in Linux is one or more files that reads standard input and writes the result to both standard output and standard output. Basically, the command output is displayed on the terminal, and the output is redirected to the file, for example, the user can use the tee command to add a new line to the root user file.

tee command basic syntax:

[command] | tee [options] [filename]

tee is basically used to display the standard output command and save it to a file, for example, we execute the 'hostnamectl command to print the hostname and other details of the system, and then save the standard output as' host_info.txt' :

$ hostnamectl | tee host_info.txt

Static hostname: ywnz

Icon name: computer-laptop

Chassis: laptop

Machine ID: 7a138ee71db94e8785d1a4dbe54dde7e

Boot ID: 34d27941b74941a9a29bc424f66613bc

Operating System: openSUSE Leap 15.2

CPE OS Name: CPE: / o: packages: leap: 15.2

Kernel: Linux 5.3.18-lp152.75-default

Architecture: x86-64

You can view the contents of the file 'host_info.txt' in the cat command:

$ cat host_info.txt

If you want to write output to multiple files, you can add a list of files separated by Spaces as an argument:

$ command | tee file1 file2… fileN

By default, the contents of the file are overwritten every time the tee command is run, and if you want to output as additional content, you need to use a or append:

$ command | tee -a file

To allow the tee command to exit normally when execution is interrupted:

$ command | tee -i file

Hide the output:

$ command | tee file >/dev/null

The tee command allows you to write to files owned by the root user or someone else. For example, if you want to add a new line to the following file, you will receive a "permission denied" error because sudo does not perform the redirection of the output:

$ sudo echo "add a newline" > /etc/sudoers

bash: /etc/sudoers: Permission denied

tee can be used:

$ echo "add a newline" | sudo tee -a /etc/sudoers

Using tee in the shell:

$ vi /tmp/testbash.sh

#! /bin/bash

LOGFILE=/tmp/test-logs-$(date +%d%m%Y)

echo "Append the logs routinely" | tee -a $LOGFILE

Relevant contents

Taiwan server CN2 line bandwidth Manual LAMP environment setup tutorial on Singapore server Hong Kong multi-IP cluster server chooses bidirectional CN2 bandwidth Other advantages of Singapore multi-IP server Singapore CN2 line cluster server and BGP line which is better Which is better, CN2 bandwidth of Korean cluster server or ordinary international bandwidth? How to improve the performance of building a storage server cluster Will overseas high-defense BGP cloud servers also lose packets? What are the reasons?
Go back

24/7/365 support.We work when you work

Support