Search code examples
bashmacosfindmd5sum

md5sum of list of files listed by find


Is it possible to get single md5 string for files that are listed by find?

This code is produce some md5 hash string but seems like it just use names of files, but I need to use content also:

find my_dir -name "*.jpg" | md5

Test on json files:

tree temp_dir

temp_dir
├── temp_1
│   ├── 071-FBA-227597_custom_faceboxes_face_bbox.json
│   └── 083-FBA-228758_custom_faceboxes_face_bbox.json
└── temp_2
    ├── 071-FBA-227597_custom_faceboxes_face_bbox.json
    └── 083-FBA-228758_custom_faceboxes_face_bbox.json

One json in temp2 is modified:

md5 temp_dir/temp_1/071-FBA-227597_custom_faceboxes_face_bbox.json
MD5 (temp_dir/temp_1/071-FBA-227597_custom_faceboxes_face_bbox.json) = 8da7666a1cf7f68b102a2ebb2ce01eae

md5 temp_dir/temp_1/083-FBA-228758_custom_faceboxes_face_bbox.json
MD5 (temp_dir/temp_1/083-FBA-228758_custom_faceboxes_face_bbox.json) = 93afe3b2b627948ff870496bf8302b85

md5 temp_dir/temp_2/071-FBA-227597_custom_faceboxes_face_bbox.json
MD5 (temp_dir/temp_2/071-FBA-227597_custom_faceboxes_face_bbox.json) = 8da7666a1cf7f68b102a2ebb2ce01eae

md5 temp_dir/temp_2/083-FBA-228758_custom_faceboxes_face_bbox.json
MD5 (temp_dir/temp_2/083-FBA-228758_custom_faceboxes_face_bbox.json) = 6308ef748f5c9a895d36bc8a71b37112

For some reason md5 on filepath list is different, is this expected?:

find temp_1 -name "*.json"
temp_1/071-FBA-227597_custom_faceboxes_face_bbox.json
temp_1/083-FBA-228758_custom_faceboxes_face_bbox.json

find temp_2 -name "*.json"
temp_2/071-FBA-227597_custom_faceboxes_face_bbox.json
temp_2/083-FBA-228758_custom_faceboxes_face_bbox.json

find temp_1 -name "*.json" | md5
ed0b14613ce97542a4e5531ff196378f

find temp_2 -name "*.json" | md5
50d0ded6eb3bf396a0b1c091c9067fdc

Also I have tried just copy temp_1 and created temp_3, but it also gives different hash, is this expected?:

find temp_3 -name "*.json"
temp_3/071-FBA-227597_custom_faceboxes_face_bbox.json
temp_3/083-FBA-228758_custom_faceboxes_face_bbox.json

find temp_3 -name "*.json" | md5
f62473085a4b32b287ead4f8f9e67e15

md5 temp_3/071-FBA-227597_custom_faceboxes_face_bbox.json
MD5 (temp_3/071-FBA-227597_custom_faceboxes_face_bbox.json) = 8da7666a1cf7f68b102a2ebb2ce01eae

md5 temp_3/083-FBA-228758_custom_faceboxes_face_bbox.json
MD5 (temp_3/083-FBA-228758_custom_faceboxes_face_bbox.json) = 93afe3b2b627948ff870496bf8302b85

Method with cat produce valid results:

find temp_1 -name "*.json" -exec cat {} \; | md5
b2abfe623e93153598d6625930f934f2

find temp_2 -name "*.json" -exec cat {} \; | md5
c64eb7a0a8749b11aa11a0312d37f81f

find temp_3 -name "*.json" -exec cat {} \; | md5
b2abfe623e93153598d6625930f934f2

Solution

  • cat $(find my_dir -name "*.jpg") | md5
    

    In case there's space in the filename

    find my_dir -name "*.jpg" -exec cat {} \; | md