# grep (search text)
grep "pattern" file.txt
grep -n "pattern" file.txt # Show line numbers
grep -i "pattern" file.txt # Case-insensitive
grep -v "pattern" file.txt # Invert match (exclude)
grep -r "pattern" /path/ # Recursive directory search
grep -E "[0-9]{3}" file.txt # Extended regex (ERE)
# sed (stream editor - replace)
sed 's/old/new/' file.txt # Replace first on each line
sed 's/old/new/g' file.txt # Replace all (global)
sed -i 's/old/new/g' file.txt # In-place edit
sed '2,5s/old/new/g' file.txt # Lines 2-5 only
sed '/pattern/s/old/new/g' file.txt # Matching lines
# awk (text processing)
awk '{print $1}' file.txt # Print 1st column
awk -F',' '{print $2}' data.csv # CSV: 2nd column
awk '{sum += $1} END {print sum}' numbers.txt # Sum column
awk 'NR > 1' file.txt # Skip header
awk '$3 > 100 {print $1, $3}' data.txt # Conditional
# Combining tools
grep "ERROR" log.txt | wc -l # Count errors
cat file.txt | sed 's/a/A/g' | grep "A"
Piping & Redirection
# Redirection
command > output.txt # Redirect stdout to file
command >> output.txt # Append to file
command 2> error.txt # Redirect stderr
command 2>&1 output.txt # Both stdout & stderr
command < input.txt # Redirect stdin
# Piping (|)
cat file.txt | grep "pattern"
ls -l | grep ".txt$"
command1 | command2 | command3
# Examples
ps aux | grep "python" # Find Python processes
find . -name "*.log" | xargs rm # Find & delete logs
cat access.log | cut -d' ' -f1 | sort | uniq -c # IP stats
# tee (pipe to file AND stdout)
command | tee output.txt
command | tee -a output.txt # Append
System & Process Management
# Disk usage
df -h # Disk space (human-readable)
du -sh folder/ # Folder size
du -h --max-depth=1 . # Size by subdirectory
# Memory & processes
free -h # Memory usage
ps aux # List all processes
ps aux | grep "python" # Find process
top # Real-time monitoring
kill 1234 # Kill process by ID
killall python # Kill all Python processes
# Search for files
find . -name "*.txt" # By name
find . -type f -size +10M # By size
find . -mtime -1 # Modified in last day
locate file.txt # Faster search (uses database)
# System info
uname -a # System info
hostname # Computer name
whoami # Current user
id # User ID & groups
uptime # System uptime
Cron Jobs (Scheduled Tasks)
# Edit cron schedule
crontab -e # Edit user crontab
sudo crontab -e # Edit root crontab
# View cron jobs
crontab -l # List user cron jobs
sudo crontab -l # List root cron jobs
# Cron syntax
# ┌───────────── minute (0-59)
# │ ┌───────────── hour (0-23)
# │ │ ┌───────────── day of month (1-31)
# │ │ │ ┌───────────── month (1-12)
# │ │ │ │ ┌───────────── day of week (0-7, 0 and 7 are Sunday)
# │ │ │ │ │
# * * * * * command
# Examples
0 9 * * * /path/to/script.sh # Daily at 9 AM
0 */4 * * * python backup.py # Every 4 hours
30 2 * * 0 /path/to/weekly_task.sh # Sundays at 2:30 AM
0 0 1 * * /path/to/monthly_task.sh # 1st of month at midnight
*/15 * * * * python check_health.py # Every 15 minutes
# Special shortcuts
@hourly /path/to/task.sh # 0 * * * *
@daily /path/to/task.sh # 0 0 * * *
@weekly /path/to/task.sh # 0 0 * * 0
@monthly /path/to/task.sh # 0 0 1 * *
@reboot /path/to/startup.sh # On system boot
# Logging cron output
0 9 * * * /path/to/script.sh >> /var/log/cron.log 2>&1