Pipes and Redirection
This is where Linux becomes truly powerful. You can chain commands together.
Output Redirection
command > file.txt # Write output to file (overwrite)
command >> file.txt # Append output to file
command 2> error.txt # Redirect errors to file
command &> all.txt # Redirect both output and errors
Input Redirection
command < file.txt # Read input from file
/dev/null — The black hole
command > /dev/null # Discard output
command 2>/dev/null # Discard errors
command &>/dev/null # Discard everything
Pipes — Chaining commands
The | character sends output of one command as input to the next:
ls -la | grep '.txt' # List only .txt files
cat /etc/passwd | grep '/bin/bash' # Find bash users
ps aux | grep nginx | wc -l # Count nginx processes
history | tail -20 # Last 20 commands
ls -la | sort -k5 -n # Sort by file size
tee — Write and pass through
command | tee output.txt # Save output to file AND print it
Practical pipeline example
cat /etc/passwd | cut -d: -f1 | sort | head -10
This: reads passwd → extracts usernames → sorts them → shows first 10