This cheatsheet is organized by topic with practical examples. Use the table of contents to quickly navigate to the sections most relevant to your current task.
File System Management
Basic File Operations
Command
Description
Example
ls
List directory contents
ls -la /home
cp
Copy files and directories
cp -r source_dir/ dest_dir/
mv
Move/rename files
mv oldname newname
rm
Remove files/directories
rm -rf directory/
find
Search for files
find / -name "*.log"
Disk & Storage Management
# Show disk space usagedf-h# Show directory sizesdu-sh /var/*# Find large files (>100MB)
find / -type f -size +100M -execls-lh{}\;# Monitor disk I/O
iostat -x 1
# Create new group
groupadd developers
# Add user to group
usermod -aG developers john
# List user groupsgroups john
# Remove user from group
gpasswd -d john developers
# View group members
getent group developers
Sudo Security
Be cautious when granting sudo privileges. Use specific commands rather than ALL when possible.
# Edit sudoers file safely
visudo
# Example sudoers entry
john ALL=(ALL) /usr/bin/systemctl, /usr/bin/apt
# Run command as another usersudo-u john whoami
Process Management
Process Monitoring
Command
Description
Usage
ps
Process status
ps aux | grep nginx
top
Interactive process viewer
top -u www-data
htop
Enhanced top
htop
kill
Terminate process
kill -9 1234
pkill
Kill by name
pkill -f nginx
System Monitoring
# CPU usage
mpstat 1
# Memory usage
free -h# I/O statistics
iostat -x 1
# Network traffic
iftop
# Real-time monitoring
watch -n 1 'df -h; echo; free -h'# Find process using port
lsof -i :80
# Show process tree
pstree -p# Nice value (priority)nice-n 10 command
renice 15 -p 1234
# Background jobsjobs
fg %1
bg %1
Always test your backups regularly. Store backups in multiple locations including off-site. Encrypt sensitive backup data.
Shell Scripting Essentials
Basic Script Template
#!/bin/bash# Script: system_info.sh# Description: Basic system information scriptset-e# Exit on error# VariablesHOSTNAME=$(hostname)DATE=$(date +%Y-%m-%d)LOG_FILE="/var/log/system_info.log"# Functions
check_disk_usage(){echo"== Disk Usage =="df-h | grep-v tmpfs
}
check_memory(){echo"== Memory Usage =="
free -h}# Main script
main(){echo"System Report for $HOSTNAME - $DATE"
check_disk_usage
check_memory
}# Execute main function
main "$@"
Cron Jobs
Cron Syntax
# Minute Hour Day Month DayOfWeek Command
# * * * * * command
# Examples:
# Every minute
* * * * * /path/command
# Every day at 2:30 AM
30 2 * * * /path/backup.sh
# Every Monday at 6 PM
0 18 * * 1 /path/script.sh
# Every 10 minutes
*/10 * * * * /path/monitor.sh
Cron Management
# Edit user crontab
crontab -e# List current crontab
crontab -l# Remove all cron jobs
crontab -r# System cron (root)sudo crontab -e# Cron directories
/etc/cron.hourly/
/etc/cron.daily/
/etc/cron.weekly/
/etc/cron.monthly/
Useful One‑Liners
# Find and delete old log files
find /var/log -name"*.log"-mtime +30 -delete# Count lines of code in project
find .-name"*.py"-execwc-l{} + | tail-1# Monitor multiple log files simultaneouslytail-f /var/log/nginx/*.log /var/log/mysql/*.log
# Create backup with timestamptar-czf"backup-$(date +%Y%m%d-%H%M%S).tar.gz" /important/data
# Kill processes matching pattern
pkill -f"python3 app.py"# SSH tunnel for MySQL access
ssh -L 3306:localhost:3306 user@dbserver
Troubleshooting Guide
SSH Connectivity Issues
# Check if SSH service is runningsudo systemctl status ssh
# Verify firewall rulessudo ufw status
# Check SSH portsudo netstat -tulpn | grep :22
# Examine SSH logssudo journalctl -u ssh -f# Test from different network
High CPU Usage
# Identify top processes
top
htop
# Check for zombie processes
ps aux | grep defunct
# Monitor system loaduptime# Check for runaway scripts
ps aux --sort=-%cpu | head# Examine application logs
Disk Full Issues
# Check disk usagedf-h# Find large directoriesdu-sh /* | sort-hr# Check for large log files
find /var/log -type f -size +100M
# Clear package cachesudo apt clean # or sudo yum clean all# Check for core dumps
find / -name"core"-size +10M
Emergency Recovery
These commands should only be used when the system is unresponsive or in critical condition.
# Force filesystem check on rebootsudo touch /forcefsck
# Enter single-user mode (emergency)sudo systemctl rescue
# Kill process by name (forceful)
pkill -9 process_name
# Remount filesystem as read-write
mount -o remount,rw /
# Emergency memory cleanupecho 3 > /proc/sys/vm/drop_caches
# Force service restartsudo systemctl reset-failed service_name