Quantcast
Channel: virtuallyPeculiar
Viewing all articles
Browse latest Browse all 167

Shell Command: Understanding tee

$
0
0
By default when you run a command, the output is reflected onto stdout. Consider this simple example,
# echo "Hello World"

The output would be:
Hello World

The output is displayed on stdout which is my terminal.

In some cases, we will have a need to run the command, view the output and simultaneously save the output to a log file. This is where tee comes in. Consider tee as a fork in a road. The output goes both ways, one to stdout and the other to a file

Syntax:
# some command | tee <file_1> <file_2>.....<file_n>

The output of some command is displayed on stdout and stored in one or more files.

tee comes with a couple of switches. The most simple tee command is:
# echo "Hello World" | tee file.txt

The output is Hello World on stdout and if you cat file.txt you will see the output Hello World
If you run the same command with a different echo text to the same file, then the existing content is replaced.

So if you want to save the existing content and append new output, then use the -a switch
# echo "Hello World" | tee file.txt
# echo "Today is a good day" | tee -a file.txt

When you run the first command, the output on stdout is Hello World and the same output is seen when you cat file.txt

When you run the second command, the output on stdout is Today is a good day and the output when you cat the file.txt is:
Hello World
Today is a good day

Next is tee with -i or --ignore-interrupts which ignores the SIGINT which would be your Ctrl+C
Sigint sends an interrupt signal to terminate a foreground process, generally when you press Ctrl+C, but you want tee to be terminated gracefully. In short, -i ignores Ctrl+C SIGINT


Viewing all articles
Browse latest Browse all 167

Trending Articles