Bash tee Command

MD Aminul Islam Feb 02, 2024
  1. the tee Command in Bash
  2. Write Multiple Files With the tee Command in Bash
  3. Append Files With the tee Command in Bash
  4. Ignore Interrupt With the tee Command in Bash
  5. Hide the Output With the tee Command in Bash
Bash tee Command

A special command in Bash is tee, which can read from standard input and write output in standard format in one or multiple files. It is also used with other commands through piping.

This article will show the tee command in detail. Also, we will look at some examples and explanations to make them easier to understand.

the tee Command in Bash

The general syntax for the tee command is tee YOUR_OPTIONS YOUR_FILE_NAME. The available options for this command are:

  1. -a - Also known as --append that tells the command not to overwrite the files and append to the provided files instead.
  2. -i - Also known as --ignore interrupts that used to ignore the interrupt signal.

Below shared a basic example of the tee command.

df -H | tee disk_usage.txt

In the example shared above, we just run the tee disk_usage.txt file. You will get the below output after you run the example code.

Filesystem      Size  Used Avail Use% Mounted on
rootfs          198G  120G   79G  61% /
none            198G  120G   79G  61% /dev
none            198G  120G   79G  61% /run
none            198G  120G   79G  61% /run/lock
none            198G  120G   79G  61% /run/shm
none            198G  120G   79G  61% /run/user
tmpfs           198G  120G   79G  61% /sys/fs/cgroup
C:\             198G  120G   79G  61% /mnt/c
E:\             178G   26G  153G  15% /mnt/e
F:\             278G  152G  127G  55% /mnt/f
G:\             278G   17G  262G   6% /mnt/g

Write Multiple Files With the tee Command in Bash

You can also write multiple files with the tee command. To do this, you can follow the below example.

command | tee file1.txt file2.txt file3.txt

In the example shared above, we write three files named file1.txt, file2.txt, and file3.txt.

Append Files With the tee Command in Bash

The command tee also enables us to append files. You can follow the below command for this purpose.

command | tee -a file.txt

As we already discussed, the -a flag indicates the append operation.

Ignore Interrupt With the tee Command in Bash

You can follow the code shared below to ignore unnecessary interrupts in the command execution.

command | tee -I file.txt

It’s very useful when stopping the command by Ctrl+C during execution.

Hide the Output With the tee Command in Bash

Don’t want tee to write output in standard mode? Just redirect it to /dev/null and follow the below command.

command | tee file.txt >/dev/null

All the codes used in this article are written in Bash. It will only be runnable in the Linux Shell environment.

MD Aminul Islam avatar MD Aminul Islam avatar

Aminul Is an Expert Technical Writer and Full-Stack Developer. He has hands-on working experience on numerous Developer Platforms and SAAS startups. He is highly skilled in numerous Programming languages and Frameworks. He can write professional technical articles like Reviews, Programming, Documentation, SOP, User manual, Whitepaper, etc.

LinkedIn