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.
Tags: bash script, commands, HowTo, ssh, tar






















Raja | January 21st, 2008 at 11:59 pm #
Damn .. gotta start my own screen casts soon
Danny | January 22nd, 2008 at 12:28 am #
take it slow man, no rush
Aik | January 22nd, 2008 at 8:40 am #
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”
Danny | January 22nd, 2008 at 9:36 am #
Hey Aik, Thanks man.
I’ve been waiting for you to show up :)….
Aik | January 22nd, 2008 at 10:44 am #
Hah, so this is a trick to dig me out eh bro?
Here’s the LPI exam. Sorry, din have ur mail
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/
Danny | January 22nd, 2008 at 5:43 pm #
Thanks bro
u da man!!
luisp | August 16th, 2008 at 4:27 am #
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.gzand it does it… but then when trying to see the fileslsit doesn’t show up, so it does not uncompress the files out of the .gz. What could I do?Danny | August 17th, 2008 at 4:18 pm #
Luisp, do you see the verbose output while running the tar command?