Lesson 13 of 17
Pipes
Connecting Commands Together
The pipe operator | takes the output of one command and feeds it as input to the next. This is one of the most powerful ideas in Linux: small tools that do one thing well, combined into powerful pipelines.
Basic Syntax
command1 | command2
The standard output (stdout) of command1 becomes the standard input (stdin) of command2.
Example
cat notes.txt | grep "daily"
This does two things:
cat notes.txtreads the file and prints itgrep "daily"reads that output and filters for lines containing "daily"
If notes.txt contains:
Learn Linux
Practice daily
Have fun
Output:
Practice daily
Longer Pipelines
Pipes can be chained indefinitely:
cat notes.txt | grep "L" | head -n 1
catreads the filegrep "L"keeps lines with "L"head -n 1keeps only the first
Output:
Learn Linux
The Philosophy
The Unix philosophy: "Write programs that do one thing and do it well. Write programs to work together." Pipes are the glue.
Instead of one giant program that filters, counts, and sorts, you have grep, wc, and sort — each tiny, each composable.
Your Task
Use a pipe to pass the contents of notes.txt through grep to find lines containing daily.
Linux shell loading...
Loading...
Click "Run" to execute your code.