Skip to content

Instantly share code, notes, and snippets.

@retroplasma
Created July 4, 2025 13:21
Show Gist options
  • Select an option

  • Save retroplasma/41560bf7e76fdd20a026892fc5326f26 to your computer and use it in GitHub Desktop.

Select an option

Save retroplasma/41560bf7e76fdd20a026892fc5326f26 to your computer and use it in GitHub Desktop.
Remotely calculate SHA256 of files where file name is [0-9a-f]{64} to see if file name == hash of file (e.g. restic, rsync.net)
#!/bin/bash
set -Efeuo pipefail
SSH='ssh user@server'
DIR='/remote/path'
#assumes that "find" and "sha256 -r" commands work on the remote machine
#count files to show live counter
file_count="$($SSH "find $(printf %q "$DIR") -type f" | grep -E '/[0-9a-f]{64}$'|wc -l)"
# hash files remotely and check
$SSH "find $(printf %q "$DIR") -type f -exec sha256 -r {} +" \
| awk -v file_count="$file_count" '
$2 ~ /\/[0-9a-f]{64}$/ {
split($2, parts, "/")
fname = parts[length(parts)]
i++
# live counter
printf "\r%u/%u", i, file_count > "/dev/stderr"
# report hash mismatch
if ($1 != fname) {
printf "\nBAD: %s (should be %s)\n", $2, $1
fail++
}
}
END {
printf "\nFinished: %u files checked. %u files mismatch.\n", i, fail > "/dev/stderr"
}
'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment