[email protected]Support 24/7 · Billing 09:00 – 00:00LinkedInXFacebook
NexonHost
Netherlands Dedicated ServersAmsterdam · Equinix AM6Germany Dedicated ServersFrankfurt · Equinix FR4Romania Dedicated ServersBucharest · VoxilityAustria Dedicated ServersVienna · Interxion VIEBulgaria Dedicated ServersSofia · TelepointFrance Dedicated ServersParis · Interxion PAR — Marseille · Digital Realty MRS3Ireland Dedicated ServersDublin · Equinix DB2Italy Dedicated ServersMilan · Irideos AvalonPoland Dedicated ServersWarsawSpain Dedicated ServersMadrid · Equinix MD2United States Dedicated ServersAshburn · Equinix DC
All articles
Getting startedJune 22, 2023

How to use tee command.

How To Use Tee Command.

In this article, we’ll cover the basics of using the tee command.

The tee command reads from the standard input and writes to both standard output and one or more files at the same time.

 

tee Command Syntax

The syntax for the tee command is as follows:

tee [OPTIONS] [FILE]
  • OPTIONS :

    • -a (–append) – Do not overwrite the files instead append to the given files.

    • -i (–ignore-interrupts) – Ignore interrupt signals.

    • Use tee –help to view all available options.

  • FILE_NAMES – One or more files. Each of which the output data is written to.

 

How to Use the tee Command

The most basic usage of the tee command is to display the standard output (stdout) of a program and write it in a file.

df -h | tee disk_usage.txt
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        2.9G     0  2.9G   0% /dev
tmpfs           2.9G     0  2.9G   0% /dev/shm
tmpfs           2.9G  297M  2.6G  11% /run
tmpfs           2.9G     0  2.9G   0% /sys/fs/cgroup
/dev/vda1        23G  1.9G   20G   9% /
tmpfs           581M     0  581M   0% /run/user/0

You can view the content of the disk_usage.txt file using the cat command .

 

Write to Multiple File

The tee command can also write to multiple files. To do so, specify a list of files separated by space as arguments:

command | tee file1.out file2.out file3.out

 

Append to File

By default, the tee command will overwrite the specified file.

command | tee -a file.out

Ignore Interrupt

To ignore interrupts use the -i (–ignore-interrupts) option. This is useful when stopping the command during execution with CTRL+C and want tee to exit gracefully.

command | tee -i file.out

 

Hide the Output

If you don’t want tee to write to the standard output, you can redirect it to /dev/null:

command | tee file.out >/dev/null

Using tee in Conjunction with sudo

Let’s say you want to write to a file that is owned by root as a sudo user. The following command will fail because the redirection of the output is not performed by sudo. The redirection is executed as the unprivileged user.

sudo echo "newline" > /etc/file.conf

The output will look something like this:

bash: /etc/file.conf: Permission denied

Simply prepend sudo before the tee command as shown below:

echo "newline" | sudo tee -a /etc/file.conf

tee will receive the output of the echo command , elevate to sudo permissions and write to the file.

Using tee in conjunction with sudo allows you to write to files owned by other users.

 

Conclusion

The tee command reads from standard input and writes it to standard output and one ore more files.

Keep reading
How to use zcat command in Linux.
September 13, 2023

How to use zcat command in Linux.

How To Use Zcat Command. The “zcat” command in Linux is a tool for displaying the contents of compressed files in the “gzip” format. It is similar to the “cat” command, which is used for indicating the contents of regular text files, but it is specifically designed for working with compressed files.

Read article
How to use cal command in Linux.
September 13, 2023

How to use cal command in Linux.

What is cal command? Sometimes, you might want to have a peek at the year’s calendar or even narrow it down to a month’s calendar. The Linux cal command is an excellent built-in tool that displays a calendar of a given year or month depending on the options passed. How Does cal Command Work?…

Read article
How to use sort command in Linux.
September 13, 2023

How to use sort command in Linux.

What is sort command? The `sort` command in Linux is part of the GNU Coreutils package. This command is used to sort lines of text in a specified file or from the standard input in an ascending/descending order. The sorted output can be written to a file or displayed on the standard output. The…

Read article
Get in Touch

Together, Let’s Build a Faster, Safer Internet.

Build scalable infrastructure with NexonHost — high-performance dedicated servers, VPS hosting, cloud hosting, and DDoS protection across Europe.

Get Started →Contact Us
[email protected]Support 24/7 · Billing 09:00 – 00:00