Skip to content

Instantly share code, notes, and snippets.

@Fyko
Last active May 17, 2024 02:34
Show Gist options
  • Save Fyko/7b19c871923ac34e172b7c0ba9fcdc52 to your computer and use it in GitHub Desktop.
Save Fyko/7b19c871923ac34e172b7c0ba9fcdc52 to your computer and use it in GitHub Desktop.
a shell script to delete Rust's `target` directory everywhere
#!/bin/sh
CYAN="\033[36m"
GREEN="\033[32m"
RESET="\033[0m"
total_size=0
count=0
BASE_DIR="."
# Allow the base directory to be specified as an argument
if [ "$1" ]; then
BASE_DIR="$1"
fi
# Print the checking message
printf "Checking directories...\n"
while IFS= read -r -d '' dir; do
if [ -f "$(dirname "$dir")/Cargo.toml" ]; then
dir_size=$(du -sb "$dir" | awk '{print $1}')
total_size=$((total_size + dir_size))
human_size=$(numfmt --to=iec --suffix=B "$dir_size")
rm -rf "$dir"
printf "Deleted ${CYAN}%s${RESET} (${CYAN}%s${RESET})\n" "$dir" "$human_size"
count=$((count + 1))
fi
done < <(find "$BASE_DIR" -name "target" -type d -prune -print0 2>/dev/null)
total_human_size=$(numfmt --to=iec --suffix=B "$total_size")
printf "Successfully deleted ${GREEN}%d${RESET} directories, saving ${GREEN}%s${RESET}\n" "$count" "$total_human_size"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment