I've been wanting to keep an eye on my file storage, and watch for files that get corrupted over time.
To that end, I'm trying to write a Linux bash / shell script to recurse through a directory and create an MD5 hashsum file for each file, in the same directory as each file. I'm not a fan of having a single file that contains all of the hashes, because it'd all fall over if that single file ever became damaged or lost.
- Directory 1
- TestFile.txt
- TestFile.txt.md5
- AnotherTestFile.wav
- AnotherTestFile.wav.md5
- Directory 2
- MyDetails.docx
- MyDetails.docx.md5
I've tried to use the md5sum
command in a variety of ways, but it always wants to either:
./Documents/Directory1/TestFile.txt
), not just the file name (e.g. TestFile.txt
).I do have a tool on Windows that does this (MD5Checker
), but it is hashing the files on my file server over the network. I'd prefer something that can run natively on a Linux OS.
Any thoughts?
It creates the MD5 file, but the file path in the hashsum file is the full file path, not the base file path.
#!/bin/bash
function md5_dir {
for file in $1/*;
do
if [[ -f "$file" && ! $file == *.md5 ]];
then
file_basename=$(basename "$file");
echo "$file" "$file_basename";
md5sum "$file" > "$file.md5";
fi;
if [[ -d "$file" ]];
then
md5_dir $file
fi;
done;
}
echo "$1"
md5_dir "$1";
find
is the go-to tool for recursively doing anything with files:
find . -type f ! -name '*.md5' -execdir sh -c 'md5sum "$1" > "$1.md5"' _ {} \;
This picks files (not named '*.md5') and runs the given inlined shell script with the filename as $1
.