macOS Sonoma Version 14.0
I have some problems with above bash code. This is my modified code that applies to me.
#!/bin/bash
# Assuming you have the backup disk connected & root privileges:
# Get the latest backup to exclude from deletion
latest=$(sudo tmutil latestbackup)
echo "Latest backup is $latest"
# The 4th line after the last space from `tmutil destinationinfo` output
# contains the mount disk name
mountpoint=$(tmutil destinationinfo | awk '{print $NF}' | sed -n '4p')
echo "The mountponint is $mountpoint"
# Delete all backups excluding the latest
backups=$(sudo tmutil listbackups)
# if you want to keep the last, say, 3 backups, pipe "sed '$d'" 3-1=2 times:
# backups=$(sudo tmutil listbackups | sed '$d' | sed '$d')
echo "$backups" | while read -r backup_path; do
timestamp=$(basename "$(dirname "$backup_path")" | sed 's/\.backup$//')
if [ "$timestamp" != "$(basename "$(dirname "$latest")" | sed 's/\.backup$//')" ]; then
echo "Deleting backup timestamp: $timestamp"
# if the output is correct, remove the echo
echo sudo tmutil delete -d "$mountpoint" -t "$timestamp"
fi
done
If the output is correct, remove the echo
in echo sudo tmutil delete -d "$mountpoint" -t "$timestamp"