In Linux, the vertical bar (|), commonly called a pipe, is one of the most powerful tools available on the command line. Pipes allow the output of one command to be used directly as the input of another, making it possible to build complex workflows from simple, single-purpose tools.
This article introduces the core concept of Linux pipes and walks through practical examples commonly used in development and system operations.
π§ Basic Concept of Linux Pipes #
A pipe connects the standard output (stdout) of one command to the standard input (stdin) of the next. Instead of writing intermediate results to a file, data flows directly from one command to another.
A simple example:
cat file.txt | grep "keyword"
Here:
cat file.txtoutputs the file contentsgrep "keyword"receives that output and filters matching lines
This approach keeps commands concise, readable, and efficient.
π Counting Specific Log Entries #
Log analysis is a classic use case for pipes. Suppose you want to count how many times "ERROR" appears in a log file:
cat error.log | grep "ERROR" | wc -l
Command breakdown:
cat error.logβ outputs the log filegrep "ERROR"β filters lines containingERRORwc -lβ counts matching lines
This pipeline turns a multi-step task into a single command.
π Finding the Top 3 File Extensions #
To identify the most common file types in a directory:
ls -l | awk '{print $NF}' | rev | cut -d. -f1 | rev | sort | uniq -c | sort -nr | head -n 3
What each stage does:
ls -lβ lists filesawk '{print $NF}'β extracts filenamesrev | cut -d. -f1 | revβ extracts file extensionssort | uniq -cβ counts occurrencessort -nrβ sorts by count (descending)head -n 3β shows the top three
This example highlights how pipes enable powerful data processing with standard tools.
π Checking Port Usage and Killing a Process #
To find and terminate the process using port 80:
sudo netstat -tuln | grep ':80' | awk '{print $7}' | cut -d/ -f1 | xargs sudo kill -9
Pipeline steps:
netstat -tulnβ lists listening portsgrep ':80'β filters port 80awk '{print $7}'β extracts PID informationcut -d/ -f1β isolates the numeric PIDxargs sudo kill -9β terminates the process
β οΈ Note: Use kill -9 with caution, as it forcefully stops processes without cleanup.
π Viewing and Saving Output Simultaneously #
Sometimes you want to see command output while also saving it to a file. The tee command enables this:
ps aux | tee output.txt | wc -l
Explanation:
ps auxβ lists all running processestee output.txtβ writes output to a file and forwards itwc -lβ counts the number of processes
Think of tee as a T-junction in the pipeline.
β Summary #
Linux pipes transform simple commands into flexible, expressive workflows. By chaining tools together:
- Temporary files become unnecessary
- Commands stay modular and readable
- Complex tasks become fast and repeatable
The real power of pipes lies in composability. Each command focuses on one job, and the pipe handles data flow. Mastering this concept turns the Linux command line into a highly efficient problem-solving toolβwhether youβre debugging logs, managing processes, or analyzing data.