27 / 55 · 19 Linux & Command Line · Pipes and Redirection← prev⊞ allnext →☰ Read as one page
4.4Essential Pipe Commands
These commands are most useful when combined with pipes:
sort
# Sort alphabetically
sort names.txt
# Sort numerically
sort -n numbers.txt
# Sort in reverse
sort -r names.txt
# Sort by specific column (e.g., 3rd column)
sort -t',' -k3 data.csv
# Sort numerically, reverse (most common in pipe chains)
sort -rn
uniq
uniq removes consecutive duplicate lines. Always sort before uniq.
# Remove duplicates
sort names.txt | uniq
# Count occurrences
sort names.txt | uniq -c
# Show only duplicates
sort names.txt | uniq -d
# Show only unique lines (no duplicates)
sort names.txt | uniq -u
wc (Word Count)
# Count lines
wc -l file.txt
# Count words
wc -w file.txt
# Count characters
wc -c file.txt
# Count lines from pipe (most common usage)
grep "ERROR" app.log | wc -l
head and tail
# First 10 lines (default)
head file.txt
# First 20 lines
head -20 file.txt
# Last 10 lines (default)
tail file.txt
# Last 50 lines
tail -50 file.txt
# Follow a file in real-time (live log monitoring)
tail -f /var/log/app/application.log
# Follow and show last 100 lines
tail -100f /var/log/app/application.log
cut
# Extract specific columns from delimited data
cut -d',' -f1,3 data.csv # Fields 1 and 3, comma-delimited
cut -d':' -f1 /etc/passwd # Username from passwd file
cut -c1-10 file.txt # First 10 characters of each line
tr (Translate)
# Convert to uppercase
echo "hello" | tr 'a-z' 'A-Z'
# Output: HELLO
# Replace spaces with newlines
echo "one two three" | tr ' ' '\n'
# Delete specific characters
echo "Hello, World!" | tr -d '!,'
# Output: Hello World
# Squeeze repeated characters
echo "hello world" | tr -s ' '
# Output: hello world
xargs
# Pass pipe output as arguments to a command
find . -name "*.spec.ts" | xargs grep "test.only"
# Delete files found by find
find /tmp -name "*.log" -mtime +7 | xargs rm
# Run a command for each line of input
cat urls.txt | xargs -I {} curl -s -o /dev/null -w "{}: %{http_code}\n" {}