10 Time-Saving Linux Automation Scripts Every User Should Know

Photo by Oleksandr Chumak on Unsplash

1. Automated Backup Script

#!/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)"
crontab -e
0 2 * * * /path/to/backup-script.sh

2. Automatic System Updates

#!/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)"
0 4 * * 1 /path/to/update-script.sh

3. File Cleanup Script

#!/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)"
0 1 * * * /path/to/cleanup-script.sh

4. Automated Log Rotation

#!/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

#!/bin/bash

rsync -avh /source/directory/ /destination/directory/

echo "File sync completed at $(date)"

6. Scheduled Reboot Script

#!/bin/bash

sudo reboot
0 3 * * 7 /path/to/reboot-script.sh

7. Automatic Disk Usage Alert

#!/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`
0 * * * * /path/to/disk-usage-alert.sh

8. Automatic Network Monitoring

#!/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

#!/bin/bash

MESSAGE="Reminder: Submit your weekly report!"

echo $MESSAGE | wall
0 9 * * 1-5 /path/to/user-notification.sh

10. System Health Check

#!/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

5 thoughts on “10 Time-Saving Linux Automation Scripts Every User Should Know

Leave a Reply

Your email address will not be published. Required fields are marked *