DEV Community

Beta Shorts
Beta Shorts

Posted on

How to Automate Everything With Bash: Beginner to Advanced Guide

A few years ago, I spent 30 minutes every morning manually checking system logs, copying backups, and cleaning up temporary files. It was tedious and unnecessary.

Then I wrote a 10-line Bash script that did everything in seconds.

If you’re still manually performing repetitive tasks, it’s time to start automating everything with Bash. This guide will take you from basic scripts to fully automated workflows, with real-world examples instead of generic tutorials.

If you need a Beginner Friendly Bash guide, check out my Bash Cheat Sheet:

👉 Download the Bash Cheat Sheet for just $3.99


1. Stop Running the Same Commands Every Day (Beginner)

❌ The Problem: Manually Running Tasks

If you find yourself typing the same command every day, you need to automate it.

✅ The Fix: Write a Simple Bash Script

Instead of typing commands repeatedly, create a script:

#!/bin/bash
echo "Backing up logs..."
cp /var/log/syslog /backup/logs/syslog_$(date +%F).log
echo "Backup complete."
Enter fullscreen mode Exit fullscreen mode

Run Your Script Automatically

chmod +x backup.sh
./backup.sh
Enter fullscreen mode Exit fullscreen mode

🔹 Why it's better: No need to remember commands—one script does it all.


2. Schedule Scripts to Run Automatically (Beginner)

Once you have a script, why run it manually? Let Linux do it for you.

Use cron to Schedule Your Scripts

Edit your crontab:

crontab -e
Enter fullscreen mode Exit fullscreen mode

Run a script every day at 2 AM:

0 2 * * * /home/user/backup.sh
Enter fullscreen mode Exit fullscreen mode

🔹 Why it's useful: Automates backups, cleanup, and system monitoring.


3. Automate File Organization (Intermediate)

❌ The Problem: Sorting Files Manually

Are you constantly renaming files, moving downloads, or organizing logs?

✅ The Fix: Automate It

Move all .jpg files from Downloads to Pictures:

#!/bin/bash
mv ~/Downloads/*.jpg ~/Pictures/
echo "Moved all images to Pictures."
Enter fullscreen mode Exit fullscreen mode

Automatically rename files with timestamps:

#!/bin/bash
for file in ~/Documents/*.txt; do
    mv "$file" "${file%.txt}_$(date +%F).txt"
done
Enter fullscreen mode Exit fullscreen mode

🔹 Why it's useful: Saves hours of manual sorting.


4. Automate System Monitoring and Alerts (Advanced)

❌ The Problem: Checking System Health Manually

You don’t need to run htop or df -h manually—let Bash monitor everything for you.

✅ The Fix: A Monitoring Script

Check Disk Space and Send Alerts

#!/bin/bash
THRESHOLD=80
usage=$(df / | awk 'NR==2 {print $5}' | sed 's/%//')

if [ "$usage" -ge "$THRESHOLD" ]; then
    echo "Warning: Disk usage is at ${usage}%!" | mail -s "Disk Alert" admin@example.com
fi
Enter fullscreen mode Exit fullscreen mode

🔹 Why it's useful: Prevents unexpected storage failures.


5. Automate Backups and Cleanup (Advanced)

❌ The Problem: Forgetting to Back Up and Delete Old Files

Stop manually copying files or cleaning up old data.

✅ The Fix: Automate It with find

Automatically Delete Files Older Than 30 Days

find /var/log -type f -mtime +30 -delete
Enter fullscreen mode Exit fullscreen mode

Backup Important Files Every Week

tar -czf /backup/home_$(date +%F).tar.gz /home/user
Enter fullscreen mode Exit fullscreen mode

🔹 Why it's useful: Runs silently in the background with cron.


6. Automate User Management (Advanced)

Create Users in Bulk from a File

#!/bin/bash
while IFS=, read -r username password; do
    useradd -m "$username"
    echo "$username:$password" | chpasswd
done < users.csv
Enter fullscreen mode Exit fullscreen mode

🔹 Why it's useful: Automates onboarding multiple users instantly.


7. Automate API Calls and Web Scraping (Advanced)

Use curl to Automate API Calls

curl -s "https://api.github.com/users/octocat" | jq '.name'
Enter fullscreen mode Exit fullscreen mode

Automate Web Scraping with wget

wget -r -np -A pdf https://example.com/reports/
Enter fullscreen mode Exit fullscreen mode

🔹 Why it's useful: Fetches real-time data automatically.


8. Automate SSH Commands for Remote Servers

Run Commands on Multiple Servers via SSH

#!/bin/bash
servers=("server1" "server2" "server3")

for server in "${servers[@]}"; do
    ssh user@$server "uptime"
done
Enter fullscreen mode Exit fullscreen mode

🔹 Why it's useful: Runs commands across all servers at once.


9. Automate Everything with One Script

If you need to fully automate your workflow, create a master script.

Example: One Script to Manage Everything

#!/bin/bash
echo "Starting system maintenance..."
echo "Updating system..."
apt update && apt upgrade -y

echo "Cleaning up old logs..."
find /var/log -type f -mtime +30 -delete

echo "Backing up home directory..."
tar -czf /backup/home_$(date +%F).tar.gz /home/user

echo "Checking disk space..."
df -h
Enter fullscreen mode Exit fullscreen mode

🔹 Why it's useful: Automates multiple system tasks in one go.


Final Thoughts: Automate and Save Hours

Bash automation can eliminate manual tasks, prevent errors, and speed up workflows.

Quick Recap:

Schedule tasks with cron

Automate file organization and cleanup

Set up system monitoring and alerts

Run SSH commands on multiple servers

Make API calls and scrape websites


Want a Structured Bash Reference?

If you need a Beginner Friendly Bash guide, check out my Bash Cheat Sheet:

👉 Download the Bash Cheat Sheet for just $3.99


Discussion: What’s the Best Bash Automation You’ve Created?

Drop a comment below and share how you use Bash to automate tasks!

Top comments (0)