Last active
January 7, 2022 14:09
-
-
Save robin-moser/60553b3650d6e2ddbf42582bf08212d6 to your computer and use it in GitHub Desktop.
Compute Checksums for all files in a directory
This file contains 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 | |
# | |
# Autor: Robin Moser, 2021 | |
# This script lists all files of a specified directory and saves it | |
# with its computed sha1 checksum in a text file. Useful for comparing, | |
# finding duplicates, logging, etc. | |
# | |
# Usage: | |
# ./generate_checksums.sh <directory> <slug> | |
# | |
# Where <directory> (without a trailing slash) specifies the directory | |
# which shall be scanned and where <slug> is an arbitrary name for that task, | |
# used in the list filename and in the log. | |
# | |
# Example: | |
# ./generate_checksums.sh /volume/media media | |
# ./generate_checksums.sh /volume/documents/private privatedocs | |
# direcotires which should be excluded | |
excludes+=(".git") | |
excludes+=("@eaDir") | |
excludes+=("excludedir - works with spaces") | |
# The path, where to store the file lists | |
path="/volume1/Backups/Synology/Checksums" | |
log="$path/log.txt" | |
# CLI arguments | |
dir="$1" | |
slug="$2" | |
if [ -z ${dir+x} ] || [ ! -d "$dir" ]; then | |
echo "Directory needs to be defined and needs to exist" | |
exit 1 | |
fi | |
if [ -z ${slug+x} ]; then | |
echo "Task name needs to be defined" | |
exit 1 | |
fi | |
# date for the filename | |
date="$(date +%Y%m%d)" | |
# date + time for the log entry | |
datetime="$(date "+%Y-%m-%dT%H:%M:%S")" | |
# the file, where the checksums with filenames should be stored | |
listfile="$path/sha1_nas_${slug}_${date}.txt" | |
# create output path if not existant | |
mkdir -p "$path" | |
# reserve the log entry, write start date | |
# Table HEAD: STARTDATE | ENDDATE | EXITCODE | TASKNAME | |
echo "$datetime | ------------------- | - | $slug" >> "$log" | |
# prepare the excludes statement | |
pattern=(-false) | |
for exclude in "${excludes[@]}"; do | |
pattern+=(-o -name "$exclude"); | |
done | |
# find all files, save the sha1sum, and write to the output file | |
find "$dir" -type d \( "${pattern[@]}" \) -prune -false \ | |
-o -type f -exec sha1sum {} \; > "$listfile" | |
# update the log file with the end date and the exit code of find | |
sed -i ".bak" "s/\($datetime\) | --* | -/\1 | $(date "+%Y-%m-%dT%H:%M:%S") | $?/" "$log" && \ | |
rm "${log}.bak" | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment