Skip to content

Instantly share code, notes, and snippets.

@kshojib
Created February 10, 2025 08:30
Show Gist options
  • Save kshojib/068a0b04ae76ff416fd9251f5b1efbfa to your computer and use it in GitHub Desktop.
Save kshojib/068a0b04ae76ff416fd9251f5b1efbfa to your computer and use it in GitHub Desktop.
Remove original backup images from wp created by Smush
#!/bin/bash
# Define the directory where your WooCommerce uploads are stored. ADJUST THIS PATH!
UPLOAD_DIR="wp-content/uploads" # Example: /var/www/html/wp-content/uploads
# Find backup files (.bak.jpg and .bak.png) within the uploads directory and its subdirectories.
find "$UPLOAD_DIR" -type f \( -name "*.bak.jpg" -o -name "*.bak.png" \) -print0 | while IFS= read -r -d $'\0' file; do
# Print the file being processed (optional, for debugging).
echo "Deleting backup file: $file"
# Remove the backup file.
rm "$file"
# Check if the deletion was successful (optional).
if [ $? -eq 0 ]; then
echo "Successfully deleted: $file"
else
echo "Error deleting: $file"
fi
done
echo "Finished processing."
# Optional: Add a command to check disk space after deletion.
# du -sh "$UPLOAD_DIR" # Shows disk usage of the uploads directory.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment