4

I succeed to obtain the md5sum of files inside archive without using file system thanks to :

tar tjf '/home/adup/mybackup.tar.bz2' | sort | grep -v '/$' |
( while read filename;
    do md5=$(tar xjOf '/home/adup/mybackup.tar.bz2' $filename | md5sum | awk '{print $1}'); 
    echo "$md5  $filename";
done)

Unfortunately what I need is to do on a remote host over ssh like :

ssh 192.9.202.44 tar tjf '/home/adup/mybackup.tar.bz2' | sort | grep -v '/$' |(  while read filename; do md5=$(tar xjOf '/home/adup/mybackup.tar.bz2' $filename | md5sum | awk '{print $1}'); echo "$md5  $filename"; done)

But like that it doesn't work, one of the tar command is interpreted locally and give me such error :

tar (child): /home/adup/mybackup.tar.bz2 : no such file

Please , is somebody can tell me how to proceed?

Thanks in advance,

piokuc
  • 25,594
  • 11
  • 72
  • 102
ad_igs
  • 63
  • 1
  • 5

3 Answers3

3

You need to pass to ssh the commands to be executed remotely as one string:

ssh 192.9.202.44 "tar tjf '/home/adup/mybackup.tar.bz2' | sort | grep -v '/\$' |(  while read filename; do md5=\$(tar xjOf '/home/adup/mybackup.tar.bz2' \$filename | md5sum | awk '{print $1}'); echo "\$md5  \$filename"; done)"

Also, make sure characters like $, which are interpreted by the local shell, are quoted.

The longer the script gets, the easier to make a mistake doing all this quoting. It can be easier to write a 'normal' shell script, copy it to the remote host with scp, and then execute with ssh.

piokuc
  • 25,594
  • 11
  • 72
  • 102
  • Great! Hours that i was trying to succeed and you did in 5 minutes. Thank you so much for adaptations. I tried to quote the whole command but not to escape $ characters, that was the trick. SOLVED! – ad_igs May 07 '13 at 16:18
  • I was doing something similar couple of weeks ago, so I knew all the traps very well :) Glad I could help. Do you mind marking the answer as correct? Thanks. – piokuc May 07 '13 at 16:22
3

Simplest and fastest solution:

ssh 192.9.202.44 "tar --to-command=md5sum -xvjf /home/adup/mybackup.tar.bz2 | paste - -"

this should produce output like:

plik_1 21576a19c7e336a86b6f37578a1b9f4d  -
plik_2 90811da0150573efaba9c9d6aa1c4ea2  -

Regards,

NiebieskiLuk
  • 131
  • 3
0

As the last version didn't get rid of the folders, here a slightly different approach for a local and remote archive:

local archive:

tar --to-command=md5sum -xvf 20180405181352.tar |grep -v /$ | paste - - > md5sum_local.txt

remote archive:

ssh user@host "cat /path/myfile.tar" | tar --to-command md5sum -xvf - | egrep -v /$ | paste - - > md5sum_remote.txt
sparkitny
  • 1,503
  • 4
  • 18
  • 23