10 Bash Scripts Every Dev Should Have in Their Toolbox

BackerLeader posted 2 min read

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!

If you read this far, tweet to the author to show them you care. Tweet a Thanks

More Posts

Top 10 Features Every CRM System Should Have

Muzzamil Abbas - May 28, 2024

Tired of writing HTML for vanilla JavaScript projects? Meet DOMSculpt.

Temi_lolu - Mar 18

6 Amazing Websites For Developers That You Don’t Know Till Yet.

paruidev - Jan 26

Why Every Developer Should Build (and Break) Their Own Side Projects

PranavVerma - Jul 21

Rotate Arrays Like a Pro, Simple Tricks Every Dev Should Know

rahul mishra - Oct 8
chevron_left