Skip to content

Instantly share code, notes, and snippets.

@thanhdevapp
Created June 14, 2025 06:27
Show Gist options
  • Save thanhdevapp/88ced0993602c8df0b1681d08c7ff418 to your computer and use it in GitHub Desktop.
Save thanhdevapp/88ced0993602c8df0b1681d08c7ff418 to your computer and use it in GitHub Desktop.
cleanup-folder.sh
#!/bin/bash
# Prompt for base directory to scan
read -rp "๐Ÿ“‚ Enter the base directory to search (e.g., ./ or /Users/you/project): " base_dir
# Check if directory exists
if [ ! -d "$base_dir" ]; then
echo "โŒ Directory '$base_dir' does not exist. Exiting."
exit 1
fi
# Prompt for folder name to delete (e.g., dist, node_modules, .angular)
read -rp "๐Ÿ—‚๏ธ Enter the folder name to search and delete (e.g., dist, node_modules): " target_name
if [ -z "$target_name" ]; then
echo "โŒ Folder name cannot be empty. Exiting."
exit 1
fi
echo "๐Ÿ” Searching for '$target_name' folders inside '$base_dir' and subdirectories..."
# Initialize result array
target_dirs=()
# Find matching folders (excluding base_dir itself)
while IFS= read -r dir; do
target_dirs+=("$dir")
done < <(find "$base_dir" -mindepth 2 -type d -name "$target_name")
# If none found
if [ ${#target_dirs[@]} -eq 0 ]; then
echo "โœ… No '$target_name' folders found."
exit 0
fi
# Show list and sizes
echo "๐Ÿ“ฆ Found the following '$target_name' folders:"
for dir in "${target_dirs[@]}"; do
du -sh "$dir"
done
# Calculate total size
total_kb=$(du -s "${target_dirs[@]}" | awk '{sum += $1} END {print sum}')
total_gb=$(awk -v kb="$total_kb" 'BEGIN {printf "%.2f", kb/1024/1024}')
echo "๐Ÿงฎ Total disk usage: $total_gb GB"
# Confirm before delete
read -p "โ—Are you sure you want to delete all these '$target_name' folders? (y/n): " confirm
if [[ "$confirm" == "y" || "$confirm" == "Y" ]]; then
for dir in "${target_dirs[@]}"; do
echo "๐Ÿ—‘๏ธ Deleting $dir..."
rm -rf "$dir"
done
echo "โœ… All '$target_name' folders have been deleted."
else
echo "โŒ Operation cancelled. No folders were deleted."
fi
@thanhdevapp
Copy link
Author

chmod +x cleanup-folder.sh &&
./cleanup-folder.sh

@thanhdevapp
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment