10 Time-Saving Linux Automation Scripts Every User Should Know
Automation is one of the greatest strengths of Linux, and with just a few scripts, you can save time and effort on repetitive tasks. Below are 10 essential automation scripts that can transform the way you work with Linux.
1. Automated Backup Script
Creating automated backups is critical for securing your important files. Use this script to back up your home directory to an external drive or a remote server.
#!/bin/bash
# Backup Directory
SOURCE="/home/yourusername/"
DESTINATION="/backup/location/"
# Run rsync to create a backup
rsync -avh --delete $SOURCE $DESTINATION
echo "Backup completed successfully at $(date)"
Save this script and schedule it using cron
:
crontab -e
0 2 * * * /path/to/backup-script.sh
This will run your backup daily at 2 AM.
2. Automatic System Updates
Keep your system up to date without manual intervention. This script ensures your system packages are updated regularly.
#!/bin/bash
sudo apt update && sudo apt upgrade -y
echo "System updated successfully at $(date)"
sudo apt update && sudo apt upgrade -y
echo "System updated successfully at $(date)"
Schedule it with cron
to run weekly:
0 4 * * 1 /path/to/update-script.sh
3. File Cleanup Script
Free up disk space by removing temporary and unnecessary files.
#!/bin/bash
# Define directories to clean
CLEANUP_DIRS=("/tmp" "/var/tmp")
for DIR in "${CLEANUP_DIRS[@]}"
do
rm -rf $DIR/*
echo "Cleaned $DIR"
done
echo "Cleanup completed at $(date)"
Run this script daily or weekly with cron
:
0 1 * * * /path/to/cleanup-script.sh
4. Automated Log Rotation
Logs can grow large over time. Use this script to rotate and compress old logs.
#!/bin/bash
LOG_DIR="/var/log/myapp/"
find $LOG_DIR -type f -mtime +7 -exec gzip {} \;
echo "Logs rotated successfully at $(date)"
5. Automated File Sync
Keep two directories in sync using this script.
#!/bin/bash
rsync -avh /source/directory/ /destination/directory/
echo "File sync completed at $(date)"
6. Scheduled Reboot Script
For servers, it’s often good practice to reboot periodically. Create a simple script for this.
#!/bin/bash
sudo reboot
Schedule it with cron
:
0 3 * * 7 /path/to/reboot-script.sh
7. Automatic Disk Usage Alert
This script checks disk usage and sends an alert if it exceeds a threshold.
#!/bin/bash
THRESHOLD=80
DISK_USAGE=$(df / | grep / | awk '{print $5}' | sed 's/%//')
if [ "$DISK_USAGE" -gt "$THRESHOLD" ]; then
echo "Disk usage is above $THRESHOLD%. Current usage: $DISK_USAGE%" | mail -s "Disk Usage Alert" your_email@example.com
fi`
Schedule it to run every hour:
0 * * * * /path/to/disk-usage-alert.sh
8. Automatic Network Monitoring
Monitor your internet connectivity and log outages.
#!/bin/bash
PING_RESULT=$(ping -c 1 google.com | grep "64 bytes" | wc -l)
if [ "$PING_RESULT" -eq 0 ]; then
echo "Network down at $(date)" >> /var/log/network-monitor.log
fi
9. Automated User Notification
Send daily reminders to users about specific tasks.
#!/bin/bash
MESSAGE="Reminder: Submit your weekly report!"
echo $MESSAGE | wall
Schedule it:
0 9 * * 1-5 /path/to/user-notification.sh
10. System Health Check
Run a quick health check and report issues.
#!/bin/bash
echo "System Health Check - $(date)"
echo "Disk Usage:"
df -h
echo "Memory Usage:"
free -m
echo "Top Processes:"
ps aux --sort=-%mem | head -5
How to Use and Schedule These Scripts
- Save the script: Create a
.sh
file (e.g.,backup.sh
) and add the script content. - Make it executable: Run
chmod +x script-name.sh
. - Schedule with
cron
: Usecrontab -e
to schedule the script.
Conclusion
These 10 automation scripts can help you save time and focus on more important tasks. Whether you’re managing backups, updating the system, or monitoring resources, these scripts provide a solid foundation for everyday Linux automation.
Connect with Me on LinkedIn
Thank you for reading! If you found these DevOps insights helpful and would like to stay connected, feel free to follow me on LinkedIn. I regularly share content on DevOps best practices, interview preparation, and career development. Let’s connect and grow together in the world of DevOps!