Add media/__init__.sh

This commit is contained in:
Mitsuo Makuda 2024-10-07 10:53:52 +01:00
parent 4e45194c81
commit cdbaee28b5

108
media/__init__.sh Normal file
View File

@ -0,0 +1,108 @@
#!/bin/bash
# Define variables
DUPLICATE_LIST="duplicates.txt" # Hard-coded duplicate list file
LOG_FILE="deleted_duplicates.log" # Main log file
TIME_LOG_FILE="deletion_times.log" # Secondary log file to track deletion time
OUTPUT_LOG_FILE="output.log" # Third log file for terminal output
SAVED_SPACE_LOG_FILE="saved_space.log" # Log file for saved space
ERROR_LOG_FILE="error.log" # Log file for errors
DRY_RUN=false # Set to true to simulate actions
CONFIRM=false # Set to true to enable confirmation before deletion
# Function to display usage
usage() {
echo "Usage: $0 [--dry-run] [--confirm]"
echo " --dry-run Simulate the file removal process."
echo " --confirm Ask for confirmation before each delete."
exit 1
}
# Parse optional flags
for arg in "$@"; do
case $arg in
--dry-run)
DRY_RUN=true
shift
;;
--confirm)
CONFIRM=true
shift
;;
*)
;;
esac
done
# Create or clear the log files
: > "$LOG_FILE"
: > "$TIME_LOG_FILE"
: > "$OUTPUT_LOG_FILE"
: > "$SAVED_SPACE_LOG_FILE"
: > "$ERROR_LOG_FILE" # Clear the error log file
# Initialize counters and total saved space
total_files_processed=0
total_files_deleted=0
total_files_skipped=0
total_saved_bytes=0
# Iterate over each duplicate file path in the list
while IFS= read -r file_path; do
total_files_processed=$((total_files_processed + 1))
if [ -f "$file_path" ]; then
# Show a confirmation prompt (if enabled)
if [ "$CONFIRM" = true ]; then
read -p "Delete $file_path? (y/n) " response
if [[ "$response" != "y" ]]; then
echo "Skipped: $file_path" | tee -a "$LOG_FILE" "$OUTPUT_LOG_FILE"
total_files_skipped=$((total_files_skipped + 1))
continue
fi
fi
# Get the size of the file before deletion
file_size=$(stat -c%s "$file_path")
total_saved_bytes=$((total_saved_bytes + file_size))
# Time the deletion or moving process
start_time=$(date +%s%3N)
# Check if we're in dry run mode
if [ "$DRY_RUN" = true ]; then
echo "[Dry Run] Would delete: $file_path (Size: $file_size bytes)" | tee -a "$LOG_FILE" "$OUTPUT_LOG_FILE"
else
# Attempt to move the file
if mv "$file_path" "$BACKUP_DIR/"; then
echo "Moved: $file_path" | tee -a "$LOG_FILE" "$OUTPUT_LOG_FILE"
total_files_deleted=$((total_files_deleted + 1))
else
echo "Error moving file: $file_path" | tee -a "$ERROR_LOG_FILE"
fi
fi
end_time=$(date +%s%3N)
duration=$((end_time - start_time))
# Log the time taken to the secondary log file
echo "File: $file_path | Time: ${duration}ms" >> "$TIME_LOG_FILE"
else
echo "File not found: $file_path" | tee -a "$LOG_FILE" "$OUTPUT_LOG_FILE"
total_files_skipped=$((total_files_skipped + 1))
fi
done < "$DUPLICATE_LIST"
# Convert total saved bytes to human-readable format
human_readable_size=$(numfmt --to=iec-i --suffix=B $total_saved_bytes)
# Log the total saved space in both formats
echo "Total space saved: $total_saved_bytes bytes ($human_readable_size)" | tee -a "$SAVED_SPACE_LOG_FILE" "$OUTPUT_LOG_FILE"
# Summary Report
echo "----- Summary Report -----" | tee -a "$OUTPUT_LOG_FILE"
echo "Total files processed: $total_files_processed" | tee -a "$OUTPUT_LOG_FILE"
echo "Total files deleted: $total_files_deleted" | tee -a "$OUTPUT_LOG_FILE"
echo "Total files skipped: $total_files_skipped" | tee -a "$OUTPUT_LOG_FILE"
echo "Duplicate removal process completed. See $LOG_FILE, $TIME_LOG_FILE, $OUTPUT_LOG_FILE, $SAVED_SPACE_LOG_FILE, and $ERROR_LOG_FILE for details."