Difference Between the cat and the tee Commands


Linux is well known for its strong command-line support. The command line utilities serve the purposes efficiently and effectively compared to the GUI methods. A group of commands usually serve the specific operation on the system. For instance, the cat and tee are Linux’s two most used text processing commands.

The cat command in Linux is to concatenate and display the contents of files. On the other hand, the tee command is used to read input from the standard input and write it both to the standard output.

This post will address the difference between cat and tee commands in Linux. The commands in this post are practiced on Ubuntu 22.04.

Difference Between the cat and the tee Commands

Until now, you have learned the explicit working of the tee and cat commands. Now, we will look into the factors to differentiate the working of both commands.

Functionality

  • The cat command is to concatenate and display the contents of files. It reads the content of files sequentially and prints it to the standard output (usually the terminal).
  • The tee command reads input from the standard input and writes it to the standard output.

Input Sources

  • The cat command reads input directly from files specified as arguments.
  • The tee command reads input from the standard input (piped data or redirected input).

Usage Scenarios

  • The cat command is commonly used when you want to view the contents of files, concatenate files, or display file contents in a pipeline.
  • The tee command is useful when viewing and saving input data simultaneously.

Until now, you have an idea of the major differences between both commands. Let’s strengthen this by discussing both commands separately.

cat Command in Linux

The cat command, short for “concatenate,” is a fundamental tool for manipulating and displaying file contents in Linux. Its simplicity and versatility make it a popular choice among users. It is commonly used to view the contents of a single file, concatenate multiple files together, or display the contents of files in a pipeline.

Let’s understand its functionality through its detailed syntax, followed by some examples.

Syntax

$ cat [OPTION] [FILE]

The syntax is described as follows:

  • [OPTION]: refers to optional command-line options that modify the behavior of the cat command.
  • [FILE]: represents one or more files whose contents you want to display or concatenate.

The following command can obtain the supported options and the detailed functionalities of the cat command:

$ cat --help

difference between cat and tee commands | linuxgenie.net
Let’s explore more about the cat command via the examples.

Example 1: Display the Content of a File

The primary usage of the cat command is to display the content of the file(s). To do so, use the cat command with the filename as follows:

$ cat genie1.txt

difference between cat and tee commands | linuxgenie.net
Two lines in the files have been printed on the terminal.

Example 2: Concatenate Multiple File

Another crucial usage of the cat command is to concatenate multiple files; the following command will concatenate the content of two files, i.e., genie1.txt and genie2.txt:

$ cat genie1.txt genie2.txt

difference between cat and tee commands | linuxgenie.net
The first two lines represent the content of “genie1.txt” and the last three lines of “genie2.txt”.

tee Command in Linux

The cat command excels at displaying file contents; it takes a different approach by allowing simultaneous display and file-saving capabilities. Moreover, it also allows you to view the input data while saving it to a file or multiple files.

Syntax

Likewise, for each command, the functionality of the tee command follows a syntax described below:

$ tee [OPTION] [FILE]

Want to get the list of options supported by the tee command? Just run the following command in your terminal:

$ tee --help

difference between cat and tee commands | linuxgenie.net

Example 1: Save Command Output to a File

The tee command can be used with other commands using the pipe character. For instance, the following command will save the output of the “ls -al” command to an external file:

$ ls -al | tee genie.txt

difference between cat and tee commands | linuxgenie.net

Example 2: Write Input Data to Multiple Files

The input data (usually the output of any command) can be saved to multiple files simultaneously. The command provided below will write the content of the df command to the files named linuxgenie.txt and linuxgenie1.txt:

$ df | tee linuxgenie.txt linuxgenie1.txt

difference between cat and tee commands | linuxgenie.net

That’s all from this guide.

Wrap Up

The cat and tee commands are the two strong text-processing utilities in Linux. The syntax of both commands is quite easy to understand and follow. The working of these commands differs in various scenarios, input sources, usage scenarios, and then functionality.

This post has provided a detailed explanation of both cat and tee commands explicitly. Moreover, you have also learned the difference between their syntax and the demonstrated examples.

Visit Linux Genie and subscribe to our newsletter.

Print Friendly, PDF & Email
Categories