1

Every month, a company sends me a hard drive - ext4 encrypted with cryptsetup - with backup files from a server. In that drive there are 1000+ files, and they almost fill up a 2Tb HDD. Each file has a corresponding .md5 file so we can check the integrity of it, but as you could imagine, I can't check every single one of them by hand in terminal, so I copy them to another hard drive and check them in Windows. On Ubuntu is there any way to check all files, or even just all files in a folder? Thanks.

2
  • @xdevs23 Well, I moved some folders to the local drive, changed permissions and ran the script. In under 2 secs it told me everything was OK but it seemed weird that it would complete so quickly. I modified a file just so it would not match, but it told me everything was ok. (sorry for the delay btw)
    – Sopa Pilon
    Feb 23, 2016 at 20:09
  • I am sorry for you mate, seems to be that the script contains an error, I tried to fix it but it didn't work so far, maybe I will get a solution later.
    – xdevs23
    Feb 24, 2016 at 5:34

3 Answers 3

3

Yes, you can. Assuming every file has just a md5 file in the same directory with just a appending .md5, create a script file (e. g. md5check.sh) with following content:

#!/bin/bash
echo "Building file list..."
$ALLFLS="$(find)"
echo "Checking all files..."
for word in $ALLFLS; do
    if [[ $(cat "$word.md5") == $(md5sum -b "$word") ]]; then
        echo "$word OK"
    else
        echo "MD5 wrong for $word"
        exit
    fi
done
echo "All files correct!"

This checks if every file. Please report if it worked. (Make sure you have changed the directory to your hard drive before executing the script and make sure you have chmod +x it)

Edit:

You can also check this page.

3

This information is weirdly hard to find, but if you want to check all the md5's against their respective files in a directory (for example Downloads) you just:

cd Downloads
md5sum -c *.md5

This will output a list of the files checked with either OK or FAILED

See: https://linux.die.net/man/1/md5sum for more information.

1
  • Note that this does not work if the shell expands the wildcard and the file list is too long
    – xdevs23
    Jan 5 at 15:59
0

If all the files are in the same directory (no subdirectories), you can run md5sum -c on every .md5 file. The following will also save errors to a file which you can check afterwards.

logerrors="md5errors.log"
for f in *.md5; do
    md5sum -c "$f" 2>> "$logerrors" || echo "ERROR on $f"
done

If your .md5 files are also to be found in subdirectories, you can use something like this:

find . -type f -name "*.md5" \
| while read f; do
    d="$(dirname "$f")"; b="$(basename "$f")"
    ( cd "$d"; md5sum -c "$b" || echo "Error on $f" | tee -a "$logerrors" )
  done

See also man md5sum. In particular, you may want to use the --quiet option:

don't print OK for each successfully verified file

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .