Linux tee Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the tee command in Linux, which allows you to redirect the output of a command to both a file and the terminal simultaneously. The lab covers the purpose and syntax of the tee command, as well as practical examples of redirecting command output and appending to existing files. The tee command is a versatile tool that is particularly useful when you want to view the output of a command while also saving it to a file for future reference.

The lab also demonstrates how to use the -a option to append the output to an existing file instead of overwriting it. This can be helpful when you want to gradually build up a log or record of command outputs over time.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/InputandOutputRedirectionGroup(["`Input and Output Redirection`"]) linux/BasicFileOperationsGroup -.-> linux/cat("`File Concatenating`") linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/InputandOutputRedirectionGroup -.-> linux/redirect("`I/O Redirecting`") linux/InputandOutputRedirectionGroup -.-> linux/tee("`Output Multiplexing`") subgraph Lab Skills linux/cat -.-> lab-422953{{"`Linux tee Command with Practical Examples`"}} linux/echo -.-> lab-422953{{"`Linux tee Command with Practical Examples`"}} linux/redirect -.-> lab-422953{{"`Linux tee Command with Practical Examples`"}} linux/tee -.-> lab-422953{{"`Linux tee Command with Practical Examples`"}} end

Understand the Purpose and Syntax of the tee Command

In this step, you will learn about the purpose and syntax of the tee command in Linux. The tee command is a powerful tool that allows you to redirect the output of a command to both a file and the terminal at the same time.

The basic syntax of the tee command is:

tee [options] [file]

Here, the [options] can include:

  • -a: Append the output to the specified file instead of overwriting it.
  • -i: Ignore interrupt signals.

To understand the purpose of the tee command, let's consider an example. Suppose you want to run a command and save its output to a file, while also displaying the output in the terminal. You can use the tee command to achieve this:

$ ls -l | tee output.txt
total 0
-rw-r--r-- 1 labex labex 0 Apr 12 12:34 output.txt

In the above example, the output of the ls -l command is redirected to both the output.txt file and the terminal.

Example output:

total 0
-rw-r--r-- 1 labex labex 0 Apr 12 12:34 output.txt

The tee command is particularly useful when you want to view the output of a command while also saving it to a file for future reference.

Redirect Command Output to a File and the Terminal

In this step, you will learn how to use the tee command to redirect the output of a command to both a file and the terminal.

Let's start by creating a sample text file:

$ echo "This is a sample text file." > sample.txt

Now, let's use the tee command to redirect the output of the cat command to both the terminal and a new file:

$ cat sample.txt | tee output.txt
This is a sample text file.

In the above example, the output of the cat sample.txt command is redirected to both the output.txt file and the terminal.

Example output:

This is a sample text file.

You can also use the -a option to append the output to an existing file instead of overwriting it:

$ echo "Adding more content." | tee -a output.txt
Adding more content.

Now, let's verify the contents of the output.txt file:

$ cat output.txt
This is a sample text file.
Adding more content.

As you can see, the output has been appended to the output.txt file.

Append Output to an Existing File with tee

In this step, you will learn how to use the tee command to append the output of a command to an existing file.

Let's start by creating a sample text file:

$ echo "This is the initial content." > sample.txt

Now, let's use the tee -a command to append the output of the echo command to the sample.txt file:

$ echo "Appending more content." | tee -a sample.txt
Appending more content.

The -a option tells the tee command to append the output to the file instead of overwriting it.

Let's verify the contents of the sample.txt file:

$ cat sample.txt
This is the initial content.
Appending more content.

As you can see, the new content has been appended to the existing file.

You can also use the tee -a command to append the output of a multi-line command to a file:

$ cat <<EOF | tee -a sample.txt
> This is the first line.
> This is the second line.
> This is the third line.
> EOF
This is the first line.
This is the second line.
This is the third line.

The output of the cat command with the here-document is appended to the sample.txt file.

Let's verify the contents of the sample.txt file again:

$ cat sample.txt
This is the initial content.
Appending more content.
This is the first line.
This is the second line.
This is the third line.

The new content has been successfully appended to the existing file.

Summary

In this lab, you learned about the purpose and syntax of the tee command in Linux, which allows you to redirect the output of a command to both a file and the terminal simultaneously. You also learned how to use the tee command to redirect the output to a file and the terminal, as well as how to append the output to an existing file instead of overwriting it. The tee command is a versatile tool that can be particularly useful when you need to view the output of a command while also saving it to a file for future reference.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like