DISQUS

DISQUS Hello! Tech-Recipes is using DISQUS, a powerful comment system, to manage its comments. Learn more.

Community Page

Tech-Recipes

Cookbook of Tech Tutorials
Jump to original thread »
Author

Tar and compress a file in one step

Started by qdideas · 9 months ago

Creating a compressed archive in UNIX in a single step is faster and is often the only method when disk space is limited.

To archive and compress a directory called ‘target’ in the current working directory into a file called target.tgz use:
tar cf - target | gzip ... Continue reading »

4 comments

  • 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.
  • 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...
  • 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
  • it would be:

    tar -cf - /home/eaips/var/fileName | gzip -c > /home/eaips/var/fileName.tar.gz

Add New Comment

Returning? Login