For a given tree, say /var/ftpd, how do I create a hash file in each folder of that tree with the contents of that folder within a given tree? sha1sum and sha512sum does not have recursion options.


FWIW the solution is:

user@host bin]$ cat mkshaindir 
cd $1
sha512sum * >.sha512sum

[user@host bin]$ find /var/ftpd -type d -print0 | xargs -0 -i  mkshaindir  {}

Note that mkshaindir, for my purposes, is a separate component because there may be a need for me to make a hash of files in a new folder, or of one that was recently changed.

The rest is left as an exercise for the reader.

Note: sha512sum will complain to STDERR about non file input (e.g. directories, block files, etc).

  • +1, though some details might be nice. (e.g. the -print0 xargs-0 to work around 'interesting' filenames. the -type d to select only directries in find, ...) – Hennes Jul 12 '15 at 6:27
  • Hennes, with apologies to Dickens, are there no googles? Are there no man pages? – Chris Jul 12 '15 at 15:38
  • Yup . Also anoying betknowers who know it all. Still, simplest to the people reading this if we pad it just a tad more. – Hennes Jul 12 '15 at 19:42
  • People come to this site for a solution or for inspiration. Those that seek a solution will cut-n-paste and not think to ask how or why. Those that seek inspiration, like myself, will read the man pages and google for themselves. The former won't care about a discussion and the latter might but would only read it if they could not "get it" by doing their own research. – Chris Jul 13 '15 at 16:34

The script should avoid to include the hashfile itself into the calculation, as saving the hashfile invalidates the hash.
Moreover, I suggest to use find -exec to save resources:

find /var/ftpd -type d -print0 ! -name .sha512sum -exec mkshaindir {} \;
  • Your statement is true if * found hidden files (aka dot files [e.g. .profile]). Ergo the reason to use a dot file for storing the hash values. A hash file is not invalidated because ONE line is failing. sha512sum -c hashfile will report a single file has failure not that the whole file is invalid. A person with some familiarity on the process would understand that it is the chicken/egg problem and ignore the one failing line in a file that was not applicable. – Chris Jul 12 '15 at 15:26
  • I was thinking of a more general case where you would check a subtree for changed files. As such, I would not exclude dot-files by principle, and secondly I'd check the subtree by a script and it's exit value (any one file changed - not a single file changed in subtree). So either I filter for .sha512sum when creating the checksum, or when checking the checksum files. But for your use case I agree generality doesn't count. – user1016274 Jul 12 '15 at 16:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.