Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I untar multiple tar files over ssh?

Tags:

bash

ssh

tar

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?

like image 966
CarpeNoctem Avatar asked Jun 01 '11 23:06

CarpeNoctem


3 Answers

(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.

like image 126
Paŭlo Ebermann Avatar answered Oct 03 '22 02:10

Paŭlo Ebermann


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
like image 43
sehe Avatar answered Oct 03 '22 01:10

sehe


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.

like image 41
bibliotechy Avatar answered Oct 03 '22 02:10

bibliotechy