#! /bin/bash total_cargo_build_space() { local -r dir="$1" local -r list="$2" local cargo_toml_path local target_path find "$dir" -type f -name Cargo.toml \ | while read -r cargo_toml_path do target_path=$(dirname "$cargo_toml_path")/target; if test -d "$target_path" then if [[ "$list" -eq 1 ]] then du -sh "$target_path" else du -s "$target_path" fi fi done \ | if [[ "$list" -eq 1 ]] then sort -h -k 1 else awk '{tot += $1} END {print tot * 1024}' | numfmt --to=iec fi } main() { local dir="$HOME" local list=0 local arg while : do arg="$1" case "$arg" in '') break ;; -l | --list) list=1 shift ;; -*) printf 'Unknown flag: "%s"\n' "$arg" >&2 exit 1 ;; *) dir="$arg" shift esac done total_cargo_build_space "$dir" "$list" } main "$@"