Skip to content

Instantly share code, notes, and snippets.

@ts1
Last active December 29, 2020 11:13
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 ts1/a85fc151c8c9a48a5427958e956b5007 to your computer and use it in GitHub Desktop.
Save ts1/a85fc151c8c9a48a5427958e956b5007 to your computer and use it in GitHub Desktop.
Time Machine cleaner - remove backups not listed in `tmutil listbackups`
#!/bin/bash
set -eo pipefail
DIR=$(tmutil machinedirectory)
backups=$(tmutil listbackups)
ls "$DIR" | while read subdir; do
if echo $subdir | grep -q '[^0-9-]'; then continue; fi
if echo $backups | grep -q $subdir; then continue; fi
dir="$DIR/$subdir"
if [ "$1" = -n ]; then
echo "$dir"
else
tmutil delete "$dir"
fi
done
@ts1
Copy link
Author

ts1 commented Oct 3, 2020

To run this in cron, give Full Disk Access permission to /usr/sbin/cron in Security and Privacy.

@ts1
Copy link
Author

ts1 commented Dec 29, 2020

This doesn't work with APFS backup introduced in Big Sur.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment