Skip to content

Instantly share code, notes, and snippets.

@fintanmm
Created December 13, 2024 10:18
Show Gist options
  • Select an option

  • Save fintanmm/19f21ee5e8042d172b5e51123dd67869 to your computer and use it in GitHub Desktop.

Select an option

Save fintanmm/19f21ee5e8042d172b5e51123dd67869 to your computer and use it in GitHub Desktop.
Find and deduplicate files in your home folder and uses notify-send to provide desktop notifications when the process starts, progresses, and completes.
#!/bin/bash
# Set the directory to the home folder
TARGET_DIR="$HOME"
# Log file to track progress (standard location)
LOG_FILE="$HOME/.local/share/logs/duperemove/duperemove.log"
# Ensure the log directory exists
mkdir -p "$HOME/.local/share/logs/duperemove"
# Permanent location for the hash file
HASH_FILE="$HOME/.duperemove_hash"
# Notify the user that the process is starting
notify-send "Duperemove" "Starting deduplication for $TARGET_DIR."
echo "$(date +'%Y-%m-%d %H:%M:%S') - Starting deduplication for $TARGET_DIR" > "$LOG_FILE"
# Step 1: Create a hash file if not already created
EXCLUDE_DIR="$HOME/.local/share/docker"
echo "$(date +'%Y-%m-%d %H:%M:%S') - Creating hash file..." >> "$LOG_FILE"
notify-send "Duperemove" "Creating hash file for deduplication."
duperemove -r -d -h --hashfile="$HASH_FILE" --exclude="$EXCLUDE_DIR" "$TARGET_DIR" >> "$LOG_FILE" 2>&1
if [ $? -ne 0 ]; then
echo "$(date +'%Y-%m-%d %H:%M:%S') - Error creating hash file." >> "$LOG_FILE"
notify-send "Duperemove" "Error creating hash file. Check log at $LOG_FILE."
exit 1
fi
echo "$(date +'%Y-%m-%d %H:%M:%S') - Hash file created." >> "$LOG_FILE"
notify-send "Duperemove" "Hash file created successfully."
# Step 2: Perform deduplication
echo "$(date +'%Y-%m-%d %H:%M:%S') - Starting deduplication..." >> "$LOG_FILE"
notify-send "Duperemove" "Performing deduplication. This may take some time."
duperemove -d --hashfile="$HASH_FILE" --exclude="$EXCLUDE_DIR" "$TARGET_DIR" >> "$LOG_FILE" 2>&1
if [ $? -ne 0 ]; then
echo "$(date +'%Y-%m-%d %H:%M:%S') - Error during deduplication." >> "$LOG_FILE"
notify-send "Duperemove" "Error during deduplication. Check log at $LOG_FILE."
exit 1
fi
# Notify the user of completion
echo "$(date +'%Y-%m-%d %H:%M:%S') - Deduplication completed." >> "$LOG_FILE"
notify-send "Duperemove" "Deduplication completed successfully for $TARGET_DIR."
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment