Skip to content

Instantly share code, notes, and snippets.

@NiceRath
Created April 22, 2024 09:04
Show Gist options
  • Save NiceRath/5ad9be0dfe3be154f58db4d73ef2cd64 to your computer and use it in GitHub Desktop.
Save NiceRath/5ad9be0dfe3be154f58db4d73ef2cd64 to your computer and use it in GitHub Desktop.
Script for recursive checksum over directory content
#!/usr/bin/env bash
# NOTES:
# perfoms md5sum on all files in the directory, sorts them and creates an overall md5sum
# WARNING: the sort order & checksum will change if you do not use the same LANG/LC_ALL!
EXCLUDES=('dir1' 'dir2/*')
set -eo pipefail
if [[ -z "$1" ]]
then
echo "USAGE:"
echo " 1 > Path to base directory"
exit 1
fi
set -u
export LANG='en_US.UTF-8'
export LC_ALL='en_US.UTF-8'
CHECK_DIR="$1"
cd "$CHECK_DIR"
exclude_args=''
for exc in "${EXCLUDES[@]}"
do
# shellcheck disable=SC2089
exclude_args="${exclude_args} -not -path \"./${exc}\""
done
# shellcheck disable=SC2090,SC2086
find . -type f $exclude_args -print0 | sort -z | xargs -0 md5sum | sort | md5sum | cut -d ' ' -f1
@NiceRath
Copy link
Author

For debugging > remove the last sort | md5sum | cut -d ' ' -f1

@NiceRath
Copy link
Author

@gries gg

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