Advanced Bash Scripting Examples for Ubuntu
Here are some powerful Bash scripting examples that will take your Ubuntu terminal skills to the next level:
1. System Monitoring Script
#!/bin/bash # System Monitor echo "===== SYSTEM MONITOR =====" echo "Date: $(date)" echo "Uptime: $(uptime -p)" echo "CPU Load: $(uptime | awk -F'load average: ' '{print $2}')" echo "Memory Usage: $(free -h | grep Mem | awk '{print $3"/"$2}')" echo "Disk Usage: $(df -h / | tail -1 | awk '{print $5}')"
Usage:
Save as
monitor.shchmod +x monitor.shRun with
./monitor.sh
2. Automated Backup Script
#!/bin/bash # Auto Backup BACKUP_DIR="/backups" SOURCE_DIR="$HOME/Documents" DATE=$(date +%Y-%m-%d) mkdir -p $BACKUP_DIR tar -czf "$BACKUP_DIR/backup_$DATE.tar.gz" "$SOURCE_DIR" echo "Backup created: $BACKUP_DIR/backup_$DATE.tar.gz"
Usage:
Save as
backup.shMake executable (
chmod +x backup.sh)Run daily with
cron
3. Network Scanner
#!/bin/bash # Network Scanner echo "Scanning local network..." for ip in 192.168.1.{1..254}; do ping -c 1 -W 1 $ip &>/dev/null && echo "$ip is UP" done
Usage:
Save as
netscan.shchmod +x netscan.shRun with
sudo ./netscan.sh
4. File Organizer
#!/bin/bash # File Organizer for file in *; do if [[ -f "$file" ]]; then ext="${file##*.}" mkdir -p "$ext" mv "$file" "$ext/" fi done
Usage:
Save as
organizer.shchmod +x organizer.shRun in any directory to sort files by extension
5. Password Generator
#!/bin/bash # Password Generator length=12 echo "Generating $length-character password:" tr -dc 'A-Za-z0-9!@#$%^&*()' < /dev/urandom | head -c $length echo
Usage:
Save as
passgen.shchmod +x passgen.shRun with
./passgen.sh
6. Log File Analyzer
#!/bin/bash # Log Analyzer logfile="/var/log/syslog" echo "Top 10 IPs in $logfile:" grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' $logfile | sort | uniq -c | sort -nr | head
Usage:
Save as
loganalyzer.shchmod +x loganalyzer.shRun with
sudo ./loganalyzer.sh
7. Automated Updates
#!/bin/bash # System Updater echo "Starting system update..." sudo apt update sudo apt upgrade -y sudo apt autoremove -y echo "Update complete!"
Usage:
Save as
update.shchmod +x update.shRun weekly with
cron
8. Weather Checker
#!/bin/bash # Weather Check city=${1:-London} curl -s "wttr.in/$city?0"
Usage:
Save as
weather.shchmod +x weather.shRun with
./weather.sh Paris
9. SSH Connection Manager
#!/bin/bash # SSH Manager echo "Available servers:" echo "1) Web Server (user@192.168.1.10)" echo "2) Database (user@192.168.1.20)" read -p "Choose server: " choice case $choice in 1) ssh user@192.168.1.10 ;; 2) ssh user@192.168.1.20 ;; *) echo "Invalid choice" ;; esac
Usage:
Save as
ssh-manager.shchmod +x ssh-manager.shRun with
./ssh-manager.sh
10. Disk Space Alert
#!/bin/bash # Disk Space Monitor THRESHOLD=90 CURRENT=$(df / | grep / | awk '{print $5}' | sed 's/%//g') if [ "$CURRENT" -gt "$THRESHOLD" ]; then echo "Warning: Disk space is at ${CURRENT}%!" | mail -s "Disk Alert" admin@example.com fi
Usage:
Save as
disk-alert.shchmod +x disk-alert.shAdd to
cronfor daily checks
Bonus: Make Scripts Executable Everywhere
# Add to ~/.bashrc export PATH=$PATH:~/scripts
Then store all scripts in ~/scripts and run them from anywhere!
Next Steps
Learn about
cronfor scheduling (crontab -e)Explore
awkandsedfor text processingStudy
grepwith regular expressionsTry making interactive scripts with
read
No comments:
Post a Comment