I am attempting to untar multiple tar files over ssh:
ssh user@hostname "cat /dir/file*.tgz" | tar xvzf -
The above only works on the first file match on the remote server. The local (dest) server only receives one file. The wildcard has been verified to match multiple files though.
Is there another way to do this?
(Edit after first try didn't work:)
Another idea, avoiding multiple ssh
calls and also scp
(since this needs at least one roundtrip between each file):
ssh user@hostname 'tar cf - /dir/file*.tgz' | tar xf - --to-command='tar xzvf -'
We use one more tar
call on the server side to wrap all the files together, a second tar
call on the client side to unwrap them again, which will then be calling tar xzv
for each entry once. This works similar to the base64-answer from sehe, but will be more efficient since it does not blow up the files.
mkdir /tmp/tars
scp 'user@hostname:/dir/file*.tgz' /tmp/tars/
foreach tarname in /tmp/tars/*.tgz; do tar xzvf "$tarname"; done
If you absolutely cannot store temp copies:
ssh user@hostname 'ls /dir/file*.tgz' | while read tarname;
do
ssh user@hostname "cat '/dir/$tarname'" | tar xzvf -
done
You could wrap this in a find call and then use the exec switch to iterate over each found file.
ssh user@hostname 'find /path/to/dir -name "*.tar.gz" -exec tar xvf "{}" ";"'
Single ssh call, no need to write bash script.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With