Bash – How to echo output of multiple commands into md5sum

bashdd

Okay so I have a somewhat large file (~200 meg) that I need to regularly hash to see if it has changed.

The catch is that there are two small fields in this file which must be ignored as they may change. Each of these fields has a known and fixed offset and size.

I have got this working using three cmp commands, however because this needs to be done regularly I'd like to do it in the most efficient way possible. Also it is problematic to have to store a backup copy of the file, and would be much better to just store a hash to compare against.

So far the closest I've got is something like this:

dd read from start of file up to offset of first field | md5sum

dd read from end of first field up to start of second field | md5sum

dd read from end of second field to end of file | md5sum

Is there any way I can redirect the output of all three of those dd commands into one md5sum instance so I can get a single hash out? I'd rather not have the three dd commands write into a temp file and then hash that as it would be a lot of I/O. I'd like to do it as efficiently as possible.

Any suggestions would be greatly appreciated!!

Best Answer

If you really want to hash the data use a subshell:

( dd_1 ; dd_2 ; dd_3 ) | md5sum

Otherwise I would suggest using Hojat's answer - hash it piecewise and do something with those "subhashes".