linuxdirectorymd5sum

How can I calculate an MD5 checksum of a directory?


I need to calculate a summary MD5 checksum for all files of a particular type (*.py for example) placed under a directory and all sub-directories.

What is the best way to do that?


The proposed solutions are very nice, but this is not exactly what I need. I'm looking for a solution to get a single summary checksum which will uniquely identify the directory as a whole - including content of all its subdirectories.


Solution

  • find /path/to/dir/ -type f -name "*.py" -exec md5sum {} + | awk '{print $1}' | sort | md5sum
    

    The find command lists all the files that end in .py. The MD5 hash value is computed for each .py file. AWK is used to pick off the MD5 hash values (ignoring the filenames, which may not be unique). The MD5 hash values are sorted. The MD5 hash value of this sorted list is then returned.

    I've tested this by copying a test directory:

    rsync -a ~/pybin/ ~/pybin2/
    

    I renamed some of the files in ~/pybin2.

    The find...md5sum command returns the same output for both directories.

    2bcf49a4d19ef9abd284311108d626f1  -
    

    To take into account the file layout (paths), so the checksum changes if a file is renamed or moved, the command can be simplified:

    find /path/to/dir/ -type f -name "*.py" -exec md5sum {} + | md5sum
    

    On macOS with md5:

    find /path/to/dir/ -type f -name "*.py" -exec md5 {} + | md5