DISQUS

Tech-Recipes: Tar and compress a file in one step

  • bofh468 · 5 years ago
    Although this probably belongs under the OpenSSH section, this is a really neat trick for snapping a tarball of a machine with limited diskspace, and storing the output on your workstation (or machine that your SSH'ing from).

    You must already have the hostkey from the remote machine (i.e., logged in at least once) otherwise this will fail. SSH allows you to connect to a remote machine and pass a commandline rather than firing up an interactive shell. The output is then dumped to your local console for further processing.

    Before you scratch your head, think of this. You have a machine that has virtually no space left (or at least, not enough to make that tarball). qmchenry's post shows you how to get tar to spool the archive to stdout:


    tar -cf - target



    What if you can capture that to your own workstation, which probably has ample space. Picture this:


    ssh user@remotemachine "cd /path;tar -cf - target | gzip -c" > /path/target.tgz



    You've just had the remote machine produce a tarball, run it through gzip, and dump the results to stdout. You just captured that at your own workstation, and saved the results.
  • Hseb72 · 5 years ago
    If you wanna try something a little bit funnier, tar and zip a file, then unzip and untar it in another directory in only one step (this is usefull to keep symlinks unchanged)

    tar -cvf - mySelection | gzip -c | (cd myNewDirectory ; gzip -dc | tar xvf -)

    make this match to your own needs...
  • Satish · 8 months ago
    I have following two steps in my script, how club them and do it in a single step ?

    tar -cf /home/eaips/var/fileName.tar /home/eaips/var/fileName
    gzip /home/eaips/var/fileName.tar
  • Quinn McHenry · 8 months ago
    it would be:

    tar -cf - /home/eaips/var/fileName | gzip -c > /home/eaips/var/fileName.tar.gz
  • fusiondog · 2 months ago
    Or 'tar zcf target.tgz' Most tar versions have had built in gzip function for over a decade at least. Over ssh it would be ssh blah@blah 'tar zcf -' > target.tgz
  • Vinayak Kamath · 4 days ago
    You can use gtar instead

    gtar -cvzf target output.tgz