Created
February 26, 2025 00:25
-
-
Save SteveRyherd/d306b6556db812f1bd141e8031764ca7 to your computer and use it in GitHub Desktop.
Backup message attachments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Set destination folder (change as needed) | |
dest=~/dest | |
# Create destination folder if it doesn't exist | |
mkdir -p "$dest" | |
# Recursively find all files in ~/Messages/Attachments/ | |
find ~/Messages/Attachments/ -type f | while IFS= read -r file; do | |
base=$(basename "$file") | |
target="$dest/$base" | |
# If a file with this name already exists, append a counter before the extension | |
if [ -e "$target" ]; then | |
# Check if the filename has an extension | |
if [[ "$base" == *.* ]]; then | |
name="${base%.*}" | |
ext="${base##*.}" | |
else | |
name="$base" | |
ext="" | |
fi | |
counter=1 | |
# Loop until we find a name that doesn't exist | |
while true; do | |
if [ -n "$ext" ]; then | |
newname="${name}_$counter.$ext" | |
else | |
newname="${name}_$counter" | |
fi | |
if [ ! -e "$dest/$newname" ]; then | |
target="$dest/$newname" | |
break | |
fi | |
counter=$((counter+1)) | |
done | |
fi | |
# Copy the file to the destination | |
cp "$file" "$target" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment