xavier
6/8/2024 - 3:48 AM

clean up files in directories then delete the directory if empty

#!/bin/bash
# Define the directory to search (you can change this to the desired directory)
DIR="/temp"

# Array of file extensions to delete
EXTENSIONS=("*.txt" "*.srt" "*.nzb" "*.sup" "*.ac3" "*.dts" "*.sample.*" "*.srr")

# Counters for deleted files and directories
deleted_files=0
deleted_directories=0

# Loop through the array and delete the files, counting each deletion
for EXT in "${EXTENSIONS[@]}"; do
    count=$(find "$DIR" -type f -name "$EXT" -print -delete | wc -l)
    deleted_files=$((deleted_files + count))
done

# Find and delete empty directories, counting each deletion
count=$(find "$DIR" -type d -empty -print -delete | wc -l)
deleted_directories=$((deleted_directories + count))

# Output the counts
echo "Deleted $deleted_files files with extensions ${EXTENSIONS[*]}."
echo "Deleted $deleted_directories empty directories."