Created
July 4, 2025 13:21
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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