Advertisement
linuxserver Bash scripts can save you hours of repetitive work. In this tutorial, we’ll create scripts that you can use in your daily workflow: cleaning temporary files, backing up directories, monitoring disk space, and organizing files. Each script is ready to run immediately.

Daily Linux Automation with Bash Scripts

5 Min Read Verified Content

Script 1: Clean Temporary Files

#!/bin/bash # Remove all .tmp and .log files in /tmp echo "Cleaning temporary files..." rm -f /tmp/*.tmp /tmp/*.log echo "Temporary files removed."

Usage:

chmod +x clean_tmp.sh ./clean_tmp.sh

Benefit: Keeps your system tidy without manual deletion.



Script 2: Daily Backup of Documents

#!/bin/bash # Backup ~/Documents to ~/backup/Documents SRC_DIR="$HOME/Documents" DEST_DIR="$HOME/backup/Documents" mkdir -p "$DEST_DIR" rsync -av --delete "$SRC_DIR/" "$DEST_DIR/" echo "Backup completed at $(date)."

Immediate insight:

  • rsync → copies only changed files efficiently

  • --delete → removes files in backup if deleted in source

  • Useful for daily document backup



Script 3: Organize Downloads by File Type

#!/bin/bash # Move files into folders by type in ~/Downloads DOWNLOADS="$HOME/Downloads" for file in "$DOWNLOADS"/*; do if [ -f "$file" ]; then EXT="${file##*.}" mkdir -p "$DOWNLOADS/$EXT" mv "$file" "$DOWNLOADS/$EXT/" echo "Moved $file to $EXT folder." fi done

Benefit: Automatically sorts files by extension (pdf, jpg, mp4, etc.), keeping your Downloads folder neat.



Script 4: Disk Usage Alert

#!/bin/bash # Alert if disk usage exceeds 80% THRESHOLD=80 USAGE=$(df / | grep / | awk '{ print $5 }' | sed 's/%//g') if [ "$USAGE" -gt "$THRESHOLD" ]; then echo "Warning! Disk usage is at ${USAGE}%" else echo "Disk usage is normal: ${USAGE}%" fi

Benefit: Prevents system issues by monitoring disk space daily. Can be automated with cron.



Script 5: Automated Log Archiving

#!/bin/bash # Archive logs from /var/log to ~/backup/logs SRC_DIR="/var/log" DEST_DIR="$HOME/backup/logs" mkdir -p "$DEST_DIR" for log in "$SRC_DIR"/*.log; do gzip -c "$log" > "$DEST_DIR/$(basename $log).gz" echo "Archived $log" done echo "All logs archived successfully."

Benefit: Keeps logs compressed and backed up, saving space and organizing historical logs.


Summary of Daily Automation Scripts

These scripts cover practical daily tasks:

  1. Cleaning temporary files → frees disk space

  2. Backing up important directories → prevents data loss

  3. Organizing downloads → keeps workflow tidy

  4. Disk monitoring → alerts you before problems occur

  5. Log archiving → maintains system logs efficiently

Advertisement
Back to Linuxserver