MacOS – List Time Machine backups of a file

macostime-machine

If I have a file or directory path, how can I list all Time Machine backups of that path?

Best Answer

Here's my attempt:

#!/bin/bash
# list Time Machine backups of a file starting with most recent

if [ "$#" -ne 1 ]; then
    echo "Usage: list_backups.sh filename"
    exit
fi

filename=`realpath "$1"`

# find root volume path
for vol in /Volumes/*; do 
    if [ "$(readlink "$vol")" = / ]; then 
        root_vol=$vol
    fi
done 

# prepend it to input path if necessary
if [[ "$filename" != /Volumes* ]]; then
    filename="$root_vol$filename"
fi

# remove /Volumes from input path
filename="${filename///Volumes}"

# loop over backups in reverse order
tmutil listbackups | tail -1000 -r |
while IFS= read -r line; do
    if [ -e "$line$filename" ]; then
        ls -ald "$line$filename"
    else
        echo "not in $line"
    fi
done