2

I'm trying to look through this: How do I get the MD5 sum of a directory's contents as one sum?, and so I'm trying:

$ find | LC_ALL=C sort | cpio -o | md5sum
25324 blocks
6631718c8856606639a4c9b1ef24d420  -

Hmm... I'd like just the hash, not anything else in the output... so assuming that "25324 blocks" has been printed to stderr, I try to redirect stderr to /dev/null:

$ find | LC_ALL=C sort | cpio -o | md5sum 2>/dev/null 
25324 blocks
6631718c8856606639a4c9b1ef24d420  -

Nope, that's not it. Let's just for tests sake, try to redirect stdout to /dev/null:

$ find | LC_ALL=C sort | cpio -o | md5sum 1>/dev/null 
25324 blocks

Ok, so the hash is gone as expected - but the "blocks" message is still there ?! Where the hell is this "25324 blocks" printed, via file descriptor 3 ?!:

$ find | LC_ALL=C sort | cpio -o | md5sum 3>/dev/null 
25324 blocks
6631718c8856606639a4c9b1ef24d420  -

Nope, that's not it... In any case, I can get just the hash with awk:

$ find | LC_ALL=C sort | cpio -o | md5sum | awk '{print $1}'
25324 blocks
6631718c8856606639a4c9b1ef24d420

but still the darn "blocks" message is printed... So how is it printed to terminal at all (as it seems not printed via either stdout or stderr), and how can I suppress that message?


EDIT: found the answer, the "blocks" message is printed by cpio actually, so the right thing to do is:

$ find | LC_ALL=C sort | cpio -o 2>/dev/null | md5sum | awk '{print $1}'
6631718c8856606639a4c9b1ef24d420

Now we have just the hash...

sdaau
  • 6,778

2 Answers2

5

The message is printed by cpio, this avoids it:

find | LC_ALL=C sort | cpio -o 2> /dev/null | md5sum | awk '{print $1}'

You’ll lose any error messages printed by cpio if you use this approach. Some versions of cpio (at least GNU and FreeBSD) support a quiet option instead:

find | LC_ALL=C sort | cpio -o --quiet | md5sum | awk '{print $1}'

To avoid losing errors with a version of cpio which doesn’t support --quiet, you could log them to a temporary file:

cpiolog=$(mktemp); find | LC_ALL=C sort | cpio -o 2> "${cpiolog}" | md5sum | awk '{print $1}'; grep -v blocks "${cpiolog}"; rm -f "${cpiolog}"
Stephen Kitt
  • 434,908
2

This message is printed by cpio. With GNU cpio or FreeBSD cpio, pass the --quiet option to suppress it.

Or ditch cpio and use the standard pax or the more common tar instead.