Bash becomes powerful when you chain commands together. Pipes, redirects, and text processing tools let you build complex data pipelines.
Essential Commands
Bash
# File operations
ls -la # List all files (long format)
cp source dest # Copy file
mv old new # Move/rename
rm file # Remove file
rm -rf directory # Remove directory (DANGEROUS!)
mkdir -p path/to/dir # Create directories
touch file # Create empty file
# File viewing
cat file # Print entire file
head -20 file # First 20 lines
tail -20 file # Last 20 lines
tail -f /var/log/syslog # Follow log file in real time
less file # Interactive viewer (q to quit)
# Searching
find / -name "*.log" # Find files by name
grep -r "error" /var/log # Search file contents
grep -i "warning" file # Case-insensitive search
grep -n "TODO" *.ts # Show line numbersPipes and Redirection
Bash
# Pipe: output of one command becomes input to next
cat access.log | grep "404" | wc -l # Count 404 errors
# Redirect stdout
echo "hello" > file.txt # Overwrite file
echo "more" >> file.txt # Append to file
# Redirect stderr
command 2> errors.log # Errors to file
command 2>&1 # Merge stderr with stdout
command > all.log 2>&1 # Both stdout and stderr
# Redirect both
command &> all.log # Same as above (bash shorthand)
# /dev/null — discard output
command > /dev/null 2>&1 # Silence everythingText Processing
Bash
# cut — extract columns
cat data.csv | cut -d',' -f1,3 # Columns 1 and 3 (comma-delimited)
# sort
cat names.txt | sort # Alphabetical
cat names.txt | sort -r # Reverse
cat data.csv | sort -t',' -k2 -n # Sort by column 2, numeric
# uniq — remove duplicates (input must be sorted)
cat file.txt | sort | uniq -c # Count occurrences
# awk — field processing
cat data.csv | awk -F',' '{print $1, $3}' # Print columns 1 and 3
cat access.log | awk '$9 == 404' # Lines where status is 404
cat access.log | awk '{sum += $10} END {print sum}' # Sum of column 10
# sed — stream editor (find/replace)
sed 's/old/new/g' file.txt # Replace in output
sed -i 's/old/new/g' file.txt # Replace in-place (modify file)
sed -n '10,20p' file.txt # Print lines 10-20
sed '/^#/d' config.txt # Remove comment lines
# jq — JSON processing
cat data.json | jq '.users[] | {name, email}'
cat data.json | jq '.items | length'
cat data.json | jq '.users[0].name'Practical Pipelines
Bash
# Count most frequent IPs in access log
cat access.log | awk '{print $1}' | sort | uniq -c | sort -rn | head -10
# Find large files
find / -type f -size +100M -exec ls -lh {} \; | sort -k5 -hr
# Monitor log errors in real time
tail -f app.log | grep --color=auto "ERROR\|WARN"
# Parse CSV and calculate average
awk -F',' '{sum += $3; count++} END {print "Average:", sum/count}' data.csv
# Remove duplicate lines preserving order
awk '!seen[$0]++' file.txt💡 Tip: Use
man commandto read the manual for any command. Usecommand --helpfor a quick summary. Thetldrtool gives practical examples.
Knowledge Check
4 questions — test your understanding
1
What does `awk '{print $1}' file` do?
2
What does the pipe `|` operator do?
3
What does `grep -r 'pattern' /dir` do?
4
What is the purpose of `/dev/null`?