How to untar over SSH

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 127.0.0.1 'tar zxvf ~/test/files.tzr.gz'

The simple fix,

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

or, (thanks Aik)

ssh 127.0.0.1 '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.

9 Responses to “How to untar over SSH”

Author comments are in a darker gray color for you to easily identify the posts author in the comments

  1. Raja says:

    Damn .. gotta start my own screen casts soon :(

  2. Danny says:

    take it slow man, no rush :)

  3. Aik says:

    There is a switch in tar that can extract to a specificied directory according to tar man page. :)

    # ssh SERVER_NAME “tar xjvf /PATH/TO/YOUR/myfile.tar.bz2 -C /YOUR-REMOTE-SERVER-DIRECTORY”

  4. Danny says:

    Hey Aik, Thanks man.

    I’ve been waiting for you to show up :) ….

  5. Aik says:

    Hah, so this is a trick to dig me out eh bro? :D

    Here’s the LPI exam. Sorry, din have ur mail :P

    El-Cheapo Community LPI Exams on 15th March 2008
    http://ditesh.gathani.org/blog/2008/01/03/el-cheapo-community-lpi-exams-on-15th-march-2008/

  6. Danny says:

    Thanks bro :) u da man!!

  7. luisp says:

    I was trying to uncompress a .tar.gz file that i uploaded to my remote directory.. I connected to it via ftp (using the macos bash terminal)… I typed ! tar zxvf blablabla.tar.gz and it does it… but then when trying to see the files ls it doesn’t show up, so it does not uncompress the files out of the .gz. What could I do?

  8. Danny says:

    Luisp, do you see the verbose output while running the tar command?

Leave a Reply

© 2008-2009 The Danesh Project
Powered by Wordpress and made by Guerrilla. Best viewed in Mozilla Firefox