Skip to content

bash script to clean up directories

#!/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."

For the most up-to-date version, follow the links below.

https://snippets.cacher.io/snippet/b554e570d8ffc0431ad9

https://gist.github.com/xavier-hernandez/f4979b4ce174068ce5428a26ae08be32

Published inCodingLinuxShell

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *