1

I want to compare local folder and folder on server, folder can contain subfolders and subfolders can contain files.

How to compute md5sum for whole folder?

Update:

One possible solution will not to compute hash for whole folder, but to compute hash for each file and then compare lists of hashes.

Lets assume we want to compare folders a and b:

tree .
.
├── a
│   ├── 1.txt
│   └── d
│       └── 2.txt
└── b
    └── 1.txt

This can show the difference:

find a -type f | sort | xargs md5 -r | cut -f1 -d " " > a.txt
find b -type f | sort | xargs md5 -r | cut -f1 -d " " > b.txt
git diff --no-index a.txt b.txt

But how to check which file corresponds to hash that is missing from one of the folders?

If I

cd a
find . -type f | sort | xargs md5 -r > ../a.txt
cd ..
cd b
find . -type f | sort | xargs md5 -r > ../b.txt
cd ..
git diff --no-index a.txt b.txt

This solves a problem, however maybe not very elegant.

mrgloom
  • 880
  • 5
  • 18
  • 29

2 Answers2

2

The solution with zip in other answer look good, but for me have some disadvantages, it need to zip the files, do not show which file is different.

Because of this I will offer one sample solution:

cd /target/directory
md5sum * >/tmp/tmp_file

If you have subdirectories and files and want to include those files too use

cd /target/directory
find . -type f -exec md5sum {} \; >/tmp/tmp_file

transfer tmp_file to other server and execute there

cd /remote_target/directory
md5sum -c /tmp/tmp_file

(if tmp_file is transferred in /tmp directory) and you will see output like this:

[root@rh1 sssd]# md5sum -c /tmp/a
./sssd.log: OK
./sssd_implicit_files.log: OK
./sssd_nss.log: OK
./a/aa: OK
1

Using rhash to output md5 hashes to file.

$ rhash -Mr -o /path/to/md5sum .

Verify hash from files.

$ rhash -c /path/to/md5sum

rhash options:

  • -M MD5: calculate and print MD5 hash sum.
  • -r Recursively process directories, specified by command line.
  • -o Set the file to output calculated hashes and verification results.
  • -c Check hash files specified by command line.

Saving directory structure to the same file.

$ find -type d -printf "; %P\n" >> /path/to/md5sum1

Compare files line by line.

$ diff md5sum1 md5sum2