I develop quite a few bash scripts to automated backups and dropbox operation in my office. One of the usual requirements is to tar (tar.gz) the files locally and later untar them on the destination server when needed. I have a few simple scripts with interactive menus which help the data center operations team with their daily backup tasks.

One of the challenges during development was the ability to untar files on the backup server over SSH. The command by default will untar to the home directory instead of the target folder output always returned success but the files were nowhere to be found. This apparently is a limitation on the tar command, it did not know where to untar the files to when being executed over SSH. Fortunately the fix was really simple.

Original command,

ssh 'tar zxvf ~/test/files.tzr.gz'

The simple fix,

ssh 'cd ~/test/ ; tar zxvf files.tar.gz'

or, (thanks Aik)

ssh 'tar zxvf files.tar.gz -C ~/test/'

Here’s a video I put together to demonstrate the above. Hopefully I got it right.


Please drop me a comment if you know a better way.