sha1sum for a directory of directories
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Forest of Spells Looping
--
Chapters
00:00 Sha1sum For A Directory Of Directories
00:29 Accepted Answer Score 24
00:46 Answer 2 Score 57
01:11 Answer 3 Score 5
03:23 Answer 4 Score 7
03:34 Thank you
--
Full question
https://superuser.com/questions/458326/s...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#bash #hashing
#avk47
ANSWER 1
Score 57
I generally like the find | xargs
pattern, like so:
find /path/to/directory -type f -print0 | xargs -0 sha1sum
You have to use the "-print0" and "-0", in case there are spaces in file names.
However, this is very similar to the find -exec cmd {} \;
pattern.
Discussion https://stackoverflow.com/questions/896808
ACCEPTED ANSWER
Score 24
Thanks to this SO post —
find . -type f \( -exec sha1sum "$PWD"/{} \; \) | awk '{print $1}' | sort | sha1sum
Warning: This code is untested! Edit this question if it's wrong and you can fix it; I'll approve your edit.
ANSWER 3
Score 7
Another trick might be to use tar to hash the file contents & metadata:
tar -cf - ./path/to/directory | sha1sum
ANSWER 4
Score 5
UPDATE: It's been a few years since I've posted this reply and in the meantime I've rewritten and improved the script I've presented here several times. I've decided to repost the new script as a brand new answer. I would highly recommend it over this one.
INTRODUCTION
I've observed that the order in which the find command outputs the found elements within a directory varies within identical directories on different partitions. If you're comparing the hashes of the same directory, you don't have to worry about that but if you're getting the hashes to ensure that no files were missed or corrupted during a copy, you need to include an additional line for sorting the content of the directory and it's elements. For example, Matthew Bohnsack's answer is quite elegant:
find ./path/to/directory/ -type f -print0 | xargs -0 sha1sum
But if you're using it to compare a copied directory to it's original, you would send the output to a txt file which you would compare to the outputted list from the other directory using Kompare or WinMerge or by simply getting the hashes of each lis. The thing is, as the order in which the find tool will output the content may vary from one directory to another, Kompare will signal many differences because the hashes weren't calculted in the same order. Not a big deal for small directories but quite annoying if you're dealing with 30000 files. Therefore, you have do the extra steps of sorting the output to make it easier to compare the hash lists between the two directories.
find ./path/to/directory/ -type f -print0 | xargs -0 sha1sum > sha1sum_list_unsorted.txt
sort sha1sum_list_unsorted.txt > sha1sum_list_sorted.txt
This would sort the output so that files with same hash are going to be on the same lines when running the differencing program (provided that no files are missing the new directory).
AND ONTO THE SCRIPT...
Here's a script that I wrote. It does what the same thing that the find/xarg answer does but it will sort the files before getting the sha1sum (keeping them in the same directory). The first line of the script finds all the files within the directory recursively. The next one sorts the results alphabetically. The following two, takes the sorted content and appends a sha1sum and quotation marks to the files in the sorted list, making a big shell script that calculates each files hash, one at a time and outputs it to content_sha1sum.txt.
#!/bin/bash
find . -type f > content.txt
sort content.txt > content_sorted.txt
awk '{print "sha1sum \""$0}' content_sorted.txt > temp.txt
awk '{print $0"\""}' temp.txt > get_sha1.sh
chmod +x get_sha1.sh
./get_sha1.sh > content_sha1sum.txt
rm content.txt
rm content_sorted.txt
rm temp.txt
rm get_sha1.sh
xdg-open content_sha1sum.txt
Hope this helps.