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:
- It lists the contents of the
/etcdirectory. - It pipes that output to
tee, which saves a copy toetc_listing.txtand also passes it along. - The output from
teeis then piped togrep, which filters for lines containing "conf".
Mastering these commands will significantly improve your efficiency on the command line.