qcow2_utils/clear_shrunk.sh
2025-09-05 17:43:56 -04:00

31 lines
681 B
Bash
Executable File

#!/bin/bash
print_and_run() {
local cmd="$*"
printf '➜ %s\n' "$cmd"
# pipefail applies to the subshell; change/remove if not desired
bash -o pipefail -c "$cmd"
}
SHRUNK="/opt/vm_drive/shrunk"
LINKS="/opt/vm_drive/*.qcow2"
ACTION="echo Would remove" # default: dry-run
DRYRUN=0
if [[ "$1" == "--dry-run" ]]; then
DRYRUN=1
shift
fi
find "$SHRUNK" -maxdepth 1 -type f -printf '%f\n' \
| grep -vxFf <(readlink -f $LINKS | xargs -n1 basename) \
| while read -r f; do
if (( DRYRUN )); then
echo "Would remove: $SHRUNK/$f"
else
echo "Removing: $SHRUNK/$f"
rm -v -- "$SHRUNK/$f"
fi
done