Reputation: 81
I'd like to calculate MD5 for all files in a tar archive. I tried tar with --to-command.
tar -xf abc.tar --to-command='md5sum'
it outputs like below.
cb6bf052c851c1c30801ef27c9af1968 -
f509549ab4eeaa84774a4af0231cccae -
Then I want to replace '-' with file name.
tar -xf abc.tar --to-command='md5sum | sed "s#-#$TAR_FILENAME#"'
it reports error.
md5sum: |: No such file or directory
md5sum: sed: No such file or directory
md5sum: s#-#./bin/busybox#: No such file or directory
tar: 23255: Child returned status 1
Upvotes: 7
Views: 5531
Reputation: 731
At first, it's better to avoid using sed
, not only because it's slow, but because $TAR_FILENAME
can contain magic chars to be interpreted by sed (you already noticed that, having to use #
instead of /
for substitution command, didnt you?). Use deadproof solution, like head
, followed by echoing actual filename.
Then, as Patrick mentions in his answer, you can't use complex commands without having them wrapped with shell, but for convenience I suggest to use built-it shell escapement ability, for bash it's printf '%q' "something"
, so the final command be like:
tar xf some.tar \
--to-command="sh -c $(printf '%q' 'md5sum | head -c 34 && printf "%s\n" "$TAR_FILENAME"')"
"34" is number of bytes before file name in md5sum output format; &&
instead of ;
to allow md5sum's error code (if any) reach tar
; printf
instead of echo
used because filenames with leading "-" may be interpreted by echo
as options.
Upvotes: 4
Reputation: 2935
You don't have a shell so this won't work (you also might see that the |
gets to md5sum as an argument). one way could be to invoke the shell yourself, but there is some hassle with nested quotes:
tar xf some.tar --to-command 'sh -c "md5sum | sed \"s|-|\$TAR_FILENAME|\""'
Upvotes: 8