Linux / Bash
Beginner
1 min read
Pipes and Standard Streams
Example
# Basic pipe: count lines in a file
cat /etc/passwd | wc -l
# More efficient equivalent:
wc -l < /etc/passwd
# Multi-stage pipeline: top 5 largest files in /var
du -sh /var/* 2>/dev/null | sort -rh | head -5
# Redirect stdout to a file (overwrite)
ls -la > filelist.txt
# Redirect stdout to a file (append)
date >> timestamps.log
# Redirect stdin from a file
sort < unsorted.txt
# Redirect stderr to a file
find / -name "*.conf" 2>errors.txt
# Redirect both stdout and stderr to same file
command > output.txt 2>&1
# Or with Bash 4+ shorthand:
command &> output.txt
# Redirect stderr to /dev/null (suppress errors)
find / -name "*.log" 2>/dev/null
# Redirect stdout to one file, stderr to another
command > stdout.txt 2>stderr.txt
# Here string: pass a string as stdin
grep "root" <<< "root:x:0:0:root:/root:/bin/bash"
# Here document: multi-line stdin
cat <<EOF
Line 1
Line 2
Line 3
EOF
# Combine pipeline with redirection
grep "ERROR" /var/log/app.log | awk '{print $1}' | sort | uniq -c > error_report.txt
Related Resources
Linux / Bash Reference
Complete tag & property list
Linux / Bash How-To Guides
Step-by-step practical guides
Linux / Bash Exercises
Practice what you've learned
More in Linux / Bash