Create separate MD5 file for each file recursively

towerofpower256 :

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:

  1. Create all of the hashes in a single file.
  2. Create separate .md5 hash files for each file, but the filenames within the md5 hash file contain the full file path (e.g. ./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?

My latest attempt (I know it's bad)

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";
that other guy :

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.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=6521&siteId=1