[email protected]Mon – Fri 9:00AM – 5:00PMLinkedInXFacebook
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 Implement Network Segmentation and Resource Isolation in Linux.
February 6, 2024

How to Implement Network Segmentation and Resource Isolation in Linux.

How to Implement Network Segmentation and Resource Isolation in Linux. Securing and managing networks in a Linux environment involves advanced practices like network segmentation and resource isolation. This guide explores these concepts, providing detailed instructions and examples for…

Read article
How to open or close ports in Linux.
February 6, 2024

How to open or close ports in Linux.

How to open or close ports in Linux. Effectively managing ports on your Linux system is crucial for both security and service accessibility. This comprehensive guide will provide a detailed walkthrough on opening and closing ports using the robust tools iptables and ufw. Understanding these…

Read article
How to use ypdomainname command.
January 23, 2024

How to use ypdomainname command.

How to use ypdomainname command. In the realm of Linux networking, the ypdomainname command plays a crucial role in the context of the Network Information Service (NIS). NIS, formerly known as Yellow Pages, is a distributed database service that facilitates the sharing of network configuration and…

Read article
Get in Touch

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

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

Get Started →Contact Us
[email protected]Mon – Fri 9:00AM – 5:00PM