Is there MD5 (or similar) to a folder? How to verify if two folders are equal?

The md5deep tool was developed for precisely this purpose. Many Linux distributions offer it in package form.


If you don't want to archive it, maybe you could do something like this

diff <(find folder1) <(find folder2)

You may have to adapt the find commands to be more accurate.

EDIT You could add -exec to your find call to compare the content of files. Something similar to this:

diff <(find folder1 -type f -exec md5sum {} \; | sort) <(find folder2 -type f -exec md5sum {} \; | sort)

Remember that you may want to adapt this.


One way to test could be to generate an md5sum based on the concatenation of all of the files in the folder and its subfolders. Bear in mind that this also requires that the files have the same names (as they must be in the same sort order). The following code should work:

#!/bin/bash

shopt -s nullglob
shopt -s globstar || { printf '%s\n' 'Bash 4 is required for globstar.' ; exit 1 ; }
(( $# == 2 )) || { printf '%s\n' "Usage: ${0##*/} olddir newdir" ; exit 2 ; }

for _file in "$1"/**/*; do [[ -f ${_file} && ! -L ${_file} ]] && _files_in_old_dir+=( "${_file}" ); done
for _file in "$2"/**/*; do [[ -f ${_file} && ! -L ${_file} ]] && _files_in_new_dir+=( "${_file}" ); done

(( ${#_files_in_old_dir[@]} )) || { printf '%s\n' 'No files in old dir.' ; exit 3 ; }
(( ${#_files_in_new_dir[@]} )) || { printf '%s\n' 'No files in new dir.' ; exit 4 ; }

_md5_old_dir=$(cat "${_files_in_old_dir[@]}" | md5sum)
_md5_new_dir=$(cat "${_files_in_new_dir[@]}" | md5sum)

{ [[ ${_md5_old_dir} == "${_md5_new_dir}" ]] && (( ${#_files_in_old_dir[@]} == ${#_files_in_new_dir[@]} )) ; } && printf '%s\n' 'Folders are identical.' || { printf '%s\n' 'Folders are not identical.' ; exit 3 ; }

If you truly care about the file names, etc, you could use a loop to compare what is in ${_files_in_old_dir} and ${_files_in_new_dir}. This should work for most cases (it at least checks the number of files in the dir and its subdirectories).