4. pipe and tee

In Linux, the command line becomes incredibly powerful when you start connecting commands. Instead of running one command, saving its output, and then running another, you can create a pipeline to pass data directly between them.

Understanding the Pipe Operator

Let's start with a command that produces a lot of output:

ls -la /etc

The list of items is likely too long to fit on your screen, making it difficult to read. While you could redirect this output to a file, a more efficient method is to send it directly to another command, like less, for easy viewing.

ls -la /etc | less

The pipe operator |, represented by a vertical bar, is the key to this process. It takes the standard output (stdout) of the command on its left and uses it as the standard input (stdin) for the command on its right. In this case, we piped the output of ls -la /etc directly into the less command. The pipe is a fundamental tool you will use constantly.

Splitting Output with the Tee Command

What if you want to see the output on your screen and save it to a file simultaneously? This is where the tee command comes in. The pipe and tee command in linux is a classic combination for logging and monitoring.

ls | tee peanuts.txt

After running this, you will see the output of ls on your terminal. If you also check the contents of peanuts.txt, you will find the exact same information. The tee command effectively splits the output stream into two directions: one to standard output and another to a specified file.

Combining Pipe and Tee

You can create even more advanced workflows by chaining these commands. A common pattern is to pipe to tee in the middle of a longer command chain. This allows you to save an intermediate result while continuing to process the data.

For example, you can use the linux pipe tee combination to view and save output before further filtering:

ls -la /etc | tee etc_listing.txt | grep "conf"

This command does three things:

  1. It lists the contents of the /etc directory.
  2. It pipes that output to tee, which saves a copy to etc_listing.txt and also passes it along.
  3. The output from tee is then piped to grep, which filters for lines containing "conf".

Mastering these commands will significantly improve your efficiency on the command line.

Sign in to save your learning progress

Sign in

Exercises

Practice makes perfect! Here are some hands-on labs to reinforce your understanding of input/output redirection and pipelines:

  1. Redirecting Input and Output in Linux - Practice controlling data flow from commands by manipulating standard output (stdout), standard error (stderr), and standard input (stdin) using operators like >, >>, 2>, and the tee command.
  2. Sequence Control and Pipeline - Learn to control command execution sequences, utilize pipelines, and leverage powerful text processing tools like cut, grep, wc, sort, and uniq.
  3. Data Stream Redirection - Learn the art of Linux stream redirection, including manipulating standard input, output, and error streams, combining outputs, and utilizing /dev/null.

These labs will help you apply the concepts of piping and redirection in real scenarios and build confidence with command-line data manipulation.

Quiz

What single character represents the pipe operator in a Linux command? Please answer with only the symbol.