Commands

15 Examples of Using the ‘tee’ Command in Linux

The tee command is a powerful utility in Linux that allows you to read input from a source and write it to both standard output and one or more files simultaneously. It can be useful in various scenarios where you want to capture or redirect output while still displaying it on the screen. In this article, we will explore 15 practical examples of using the ‘tee’ command in Linux, along with detailed descriptions and sample command outputs.

1. Basic Output Redirection

The simplest usage of ‘tee’ involves redirecting the output of a command to a file while still displaying it on the screen.

$ ls | tee file.txt
2. Appending to a File

To append the output to an existing file instead of overwriting it, use the ‘-a‘ flag.

$ echo "Additional text" | tee -a file.txt

Example output:

Existing text
Additional text
3. Writing to Multiple Files

‘tee’ can write the output to multiple files simultaneously.

$ echo "Data" | tee file1.txt file2.txt
4. Discarding Output

You can discard the output on the screen and write it only to a file using the ‘/dev/null’ file.

$ command >/dev/null | tee file.txt
5. Ignoring Errors

To ignore error messages and capture only the standard output, use the ‘2>/dev/null’ redirection.

$ command 2>/dev/null | tee file.txt
6. Displaying Command Execution Time

By combining ‘tee’ with the ‘time’ command, you can measure the execution time of a command while still displaying its output.

$ time ls | tee file.txt

Example output:

file1.txt
file2.txt
...

real    0m0.005s
user    0m0.001s
sys     0m0.003s
7. Piping Output to Another Command

‘tee’ can be used to redirect output to another command in a pipeline.

$ ls | tee file.txt | grep file1
8. Verbose Output with Timestamps

By using the ‘ts’ command in conjunction with ‘tee,’ you can add timestamps to the output.

$ ls | tee >(ts '[%Y-%m-%d %H:%M:%S]') | grep file1
9. Displaying Output in Real-Time

‘tee’ allows you to display the output in real-time while capturing it in a file for later use.

$ ping google.com | tee -a ping.log

Output (on the screen and in ping.log):

PING google.com (172.217.169.78) 56(84) bytes of data.
64 bytes from google.com (172.217.169.78): icmp_seq=1 ttl=53 time=32.2 ms
...
10. Redirecting Standard Error

To redirect standard error to a file while still displaying it on the screen, use the ‘2>&1’ redirection.

$ command 2>&1 | tee error.log
11. Limiting Output to a Number of Lines

You can limit the output to a specific number of lines using the ‘head’ command in combination with ‘tee.’

$ ls | tee >(head -n 5) | grep file1
12. Creating a Backup Copy

To create a backup of a file while modifying it, you can use ‘tee’ to write the output to a different file.

$ cat file.txt | tee file.txt.backup | grep "pattern" > file.txt
13. Formatting Output

You can format the output using tools like ‘sed’ or ‘awk’ in combination with ‘tee.’

$ command | tee >(sed 's/foo/bar/g') | awk '{print $1}'
14. Logging Command Output

By using ‘tee’ with the ‘script’ command, you can log the entire output of a command to a file.

$ script -c "command" | tee output.log
15. Sending Output to Multiple Commands

‘tee’ allows you to send output to multiple commands simultaneously.

$ command | tee >(command1) >(command2) >(command3) > /dev/null

Example output:

Result of command1
Result of command2
Result of command3

These examples demonstrate the versatility and usefulness of the ‘tee’ command in Linux. By mastering its various applications, you can efficiently manage and manipulate command output in your day-to-day Linux operations.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

CAPTCHA


This site uses Akismet to reduce spam. Learn how your comment data is processed.

Check Also
Close
Back to top button