10 Essential Bash Scripts Every Developer Needs

Photo by Markus Spiske on Pexels
10 Essential Bash Scripts Every Developer Needs
Bash scripting is one of the most powerful skills a developer can cultivate. Whether you are deploying code, managing servers, or automating routine tasks, a well-written bash script saves hours of repetitive work. In this article, we walk through ten essential bash scripts that every developer should have in their toolkit. Each script is production-ready, thoroughly commented, and designed to solve real-world problems you face daily.
1. Automated Git Repository Backup
Losing local work is every developer’s nightmare. This script automates backing up all your Git repositories to a remote server or cloud storage. It iterates through a directory of repos, pulls the latest changes, creates a compressed archive, and uploads it via rsync or scp.
#!/bin/bash
# Automated Git Backup Script
BACKUP_DIR="/backups/git"
REMOTE_USER="user"
REMOTE_HOST="backup.example.com"
REMOTE_PATH="/remote/backups/git"
for repo in /home/user/projects/*/; do
cd "$repo"
tar -czf "$BACKUP_DIR/$(basename $repo).tar.gz" .
rsync -avz "$BACKUP_DIR/$(basename $repo).tar.gz" \
"$REMOTE_USER@$REMOTE_HOST:$REMOTE_PATH"
done
Schedule this script with a cron job and you will never worry about lost repositories again. Add error handling with set -e and logging to ensure you always know the backup status.
2. System Health Check
A comprehensive health check script reports CPU load, memory usage, disk space, network connectivity, and running services in a single glance. This is indispensable for sysadmins and developers who manage their own servers. Run it manually when troubleshooting or schedule it to run hourly and pipe output to a monitoring log.
What It Monitors
- CPU load average (1, 5, and 15 minutes)
- Memory and swap usage
- Disk usage per mounted partition
- Top 5 processes by memory consumption
- Active network connections
The output is color-coded: green for healthy, yellow for warning, red for critical thresholds. Pipe it to a log file and set up alerting for any red condition. This script is especially useful when onboarding new servers to verify they meet baseline performance expectations.
3. Bulk File Renamer
Renaming hundreds of files manually is tedious and error-prone. This script accepts a pattern and replacement string, then performs a bulk rename with a dry-run mode so you can preview changes before applying them. The dry-run mode is the killer feature here — it prevents costly mistakes before they happen.
#!/bin/bash
# Bulk File Renamer
DRY_RUN=true
for file in *.log; do
new_name="${file//2024/2025}"
if [ "$DRY_RUN" = true ]; then
echo "Would rename: $file -> $new_name"
else
mv "$file" "$new_name"
fi
done
Add regex support by integrating sed or perl rename commands for more complex transformations. The script handles prefixes, suffixes, extension changes, and date-based naming conventions with minimal modification.
4. Docker Log Cleaner
Docker containers generate massive log files that can fill your disk in days. This script prunes old logs, removes unused images, and reclaims disk space with a single command. It respects a configurable retention period so you never lose critical debugging history.
- Truncates container logs larger than 100MB
- Removes dangling images and volumes
- Reports space reclaimed before and after cleanup
- Can be safely run daily via cron without impacting running containers
Combine this script with Docker’s built-in log rotation configuration in daemon.json for a complete log management strategy that keeps your disk usage predictable.
5. Database Backup and Rotation
Automated database backups are non-negotiable for any production application. This script dumps MySQL or PostgreSQL databases, compresses them, and implements a rotation policy that keeps only the last N days of backups. Without rotation, backup directories grow unbounded and eventually cause the very outage they are meant to prevent.
#!/bin/bash
# Database Backup with Rotation
DB_NAME="myapp"
BACKUP_DIR="/backups/db"
RETENTION_DAYS=30
mysqldump --single-transaction "$DB_NAME" | gzip > \
"$BACKUP_DIR/${DB_NAME}_$(date +%F).sql.gz"
find "$BACKUP_DIR" -name "*.sql.gz" -mtime +$RETENTION_DAYS -delete
Extend this script to support multiple databases, S3 uploads, and Slack notifications on failure. The --single-transaction flag ensures a consistent snapshot without locking tables, making it safe for production databases with active writes.
6. SSL Certificate Expiry Checker
Expired SSL certificates cause downtime and erode user trust. This script checks certificate expiry dates for all your domains and sends warnings when a certificate is within 30 days of expiration. It uses OpenSSL’s s_client to connect to each domain and parse the certificate’s end date, then compares it against the current date to determine remaining validity.
7. Deployment Rollback Script
Every deployment can fail. A rollback script ensures you can revert to the previous working version in seconds. This script maintains a stack of the last five deployments and can instantly symlink the previous release. The key design principle is simplicity — a rollback during an incident is not the time to debug a complex script.
8. Log Analyzer with Alerting
Parse application logs for error patterns and send alerts via email or webhook. The script supports tailing log files in real time or scanning historical logs with grep and awk for anomaly detection. Define your own error patterns and severity levels, and the script will aggregate matches and send a digest on schedule.
9. Environment Variable Loader
Loading environment variables across multiple projects is tedious. This script loads .env files safely, validates required variables are set, and warns about missing or misconfigured values before your application starts. It also detects duplicate or conflicting variable definitions across multiple .env files.
10. CI/CD Pipeline Helper
A versatile helper script that integrates with any CI/CD platform. It handles semantic version bumping, changelog generation, artifact packaging, and deployment tagging. Use it with GitHub Actions, GitLab CI, or Jenkins. The script reads the commit history since the last tag, determines the appropriate version bump (major, minor, or patch), generates a changelog entry, and creates a new annotated tag.
How to Use These Scripts Safely
Always review scripts downloaded from the internet before running them. Follow these best practices:
- Run scripts in a test environment first before using them on production systems
- Use
set -eto exit on errors andset -uto catch unset variables - Quote all variables to prevent word splitting and pathname expansion
- Validate inputs and sanitize user-supplied data to prevent injection attacks
- Log all output for auditing and debugging with timestamps
Download the Complete Script Collection
All ten scripts are available as a single downloadable package on freefile.store. Each script includes comprehensive comments, usage instructions, and example configurations. Download the collection today and supercharge your development workflow with battle-tested automation.