This tutorial is designed to teach you how to use pipes (|
) and tee
in the Linux shell for efficient data processing. By the end of this tutorial, you'll understand how to chain commands together and output data to multiple places simultaneously.
You'll learn:
- What pipes and tee
are
- How to use pipes to chain commands
- How to use tee
to write to multiple streams
Prerequisites:
- Basic knowledge of Linux command line
- Access to a Linux terminal
|
)A pipe, represented by the |
symbol, links the stdout (standard output) of one command to the stdin (standard input) of another. This allows for commands to be chained together.
Example:
ls | grep 'txt'
In this example, the ls
command lists all files in the current directory. The output is then passed through the pipe to the grep
command, which filters out only lines containing 'txt'.
The tee
command is used to read from standard input and write to standard output and files.
Example:
echo 'Hello, World' | tee file1.txt
In this example, the echo
command prints 'Hello, World', which is passed to the tee
command. tee
then writes this to file1.txt
and to standard output.
# List all files and filter out .txt files
ls | grep '.txt'
tee
to write to a file and standard output:# Print 'Hello, World' and write to file1.txt
echo 'Hello, World' | tee file1.txt
tee
:# Print 'Hello, World' and write to file1.txt and file2.txt
echo 'Hello, World' | tee file1.txt file2.txt
tee
:# Append 'Goodbye, World' to file1.txt
echo 'Goodbye, World' | tee -a file1.txt
In this tutorial, you've learned how to use pipes and tee
to chain commands and write to multiple streams simultaneously.
Next steps for learning could include exploring other ways to manipulate streams in the Linux command line, such as redirection (>
and <
).
tee
to write to two files at once.tee
to append a line of text to a file.Solutions:
1. ls | grep '.txt' | wc -l
- This will list files, filter out .txt files, and then count the number of .txt files.
2. echo 'Hello, World' | tee file1.txt file2.txt
- This will write 'Hello, World' to both file1.txt
and file2.txt
.
3. echo 'Goodbye, World' | tee -a file1.txt
- This will append 'Goodbye, World' to file1.txt
.
For further practice, try to come up with your own chains of commands and experiment with different uses of tee
.