title: Create separate MD5 file for each file recursively
body: <p>I've been wanting to keep an eye on my file storage, and watch for files that get corrupted over time.</p>
  
  <p>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.</p>
  
  <pre><code>- Directory 1
      - TestFile.txt
      - TestFile.txt.md5
      - AnotherTestFile.wav
      - AnotherTestFile.wav.md5
  - Directory 2
      - MyDetails.docx
      - MyDetails.docx.md5
  </code></pre>
  
  <p>I've tried to use the <code>md5sum</code> command in a variety of ways, but it always wants to either:</p>
  
  <ol>
  <li>Create all of the hashes in a single file.</li>
  <li>Create separate .md5 hash files for each file, but the filenames within the md5 hash file contain the full file path (e.g. <code>./Documents/Directory1/TestFile.txt</code>), not just the file name (e.g. <code>TestFile.txt</code>).</li>
  </ol>
  
  <p>I do have a tool on Windows that does this (<code>MD5Checker</code>), 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.</p>
  
  <p>Any thoughts?</p>
  
  <h2>My latest attempt (I know it's bad)</h2>
  
  <p>It creates the MD5 file, but the file path in the hashsum file is the full file path, not the base file path.</p>
  
  <pre><code>#!/bin/bash
  
  function md5_dir {
  for file in $1/*;
  do
          if [[ -f "$file" &amp;&amp; ! $file == *.md5 ]];
          then
                  file_basename=$(basename "$file");
                  echo "$file" "$file_basename";
                  md5sum "$file" &gt; "$file.md5";
          fi;
          if [[ -d "$file" ]];
          then
                  md5_dir $file
          fi;
  done;
  }
  
  
  
  echo "$1"
  md5_dir "$1";
  </code></pre>