Skip to content

Instantly share code, notes, and snippets.

@Fyko
Created December 14, 2023 05:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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
while IFS= read -r -d '' dir; do
if [ -f "$(dirname "$dir")/Cargo.toml" ]; then
dir_size=$(du -sb "$dir" | cut -f1)
total_size=$((total_size + dir_size))
rm -rf "$dir"
echo -e "Deleted ${CYAN}$dir${RESET}"
count=$((count + 1))
fi
done < <(find . -name "target" -type d -prune -print0 2>/dev/null)
size=$(numfmt --to=iec --suffix=B $total_size)
echo -e "Successfully deleted ${GREEN}$count${RESET} directories, saving ${GREEN}$size${RESET}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment