Created
June 14, 2025 06:27
-
-
Save thanhdevapp/88ced0993602c8df0b1681d08c7ff418 to your computer and use it in GitHub Desktop.
cleanup-folder.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
chmod +x cleanup-folder.sh &&
./cleanup-folder.sh