Whether you're managing servers, automating tasks, or just trying to save time, Bash scripts are powerful allies. Here are 10 essential Bash scripts that every developer should keep handy to boost productivity and simplify daily tasks.
1. Auto Backup Script
Automatically back up important directories or databases to a remote server or local storage.
!/bin/bash
tar -czf /backup/$(date +%F)-project.tar.gz /var/www/project
2. Server Health Check Script
Monitor CPU, memory, and disk usage and get notified if thresholds are exceeded.
!/bin/bash
TOP_CPU=$(top -bn1 | grep "Cpu" | awk '{print $2}')
FREE_MEM=$(free -m | awk '/Mem:/ { print $4 }')
df -h > /tmp/disk_usage.txt
3. Git Auto Commit and Push Script
Save and sync your work with one command.
!/bin/bash
git add .
git commit -m "$1"
git push origin main
4. MySQL Database Dump Script
Create a dump of your MySQL database for backups or migrations.
!/bin/bash
mysqldump -u root -pYourPassword database_name > backup.sql
5. Project Bootstrapper
Spin up a new project directory with folders and initial files.
!/bin/bash
mkdir $1 && cd $1
mkdir src tests docs
code .
6. Service Monitor Script
Check if a service like NGINX or MySQL is running and restart it if it's not.
!/bin/bash
SERVICE=$1
if ! pgrep -x "$SERVICE" > /dev/null
then
systemctl restart $SERVICE
fi
7. SSH Key Setup Script
Quickly generate and copy SSH keys to a remote server.
!/bin/bash
ssh-keygen -t rsa -b 4096 -C "$1"
ssh-copy-id user@$2
8. Log Rotation Script
Archive and compress old logs to keep storage in check.
!/bin/bash
find /var/log -name "*.log" -mtime +7 -exec gzip {} \;
9. Code Cleaner Script
Remove unnecessary files like .DS_Store
, Thumbs.db
, or compiled Python files.
!/bin/bash
find . -type f \( -name '*.pyc' -o -name '*.DS_Store' -o -name 'Thumbs.db' \) -delete
10. URL Status Checker
Check if a website or API is online.
!/bin/bash
URL=$1
STATUS=$(curl -o /dev/null -s -w "%{http_code}\n" $URL)
echo "Status for $URL: $STATUS"
Final Thoughts
These Bash scripts are just the tip of the iceberg. They can save time, improve consistency, and automate tedious tasks. Save them, modify them to fit your workflow, and add them to your developer toolbox today!