Tar and compress a file in one step

Contributor Icon Contributed by qmchenry Date Icon September 15, 2003  
Tag Icon Tagged: UNIX

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 -c > target.tgz

The - in the place of a tar output filename directs tar to send its output to the standard output which is redirected to gzip. Substituting bzip2, when available in your operating system, will also work and generally provides better compression ratios.

Previous recipe | Next recipe |
 

Viewing 2 Comments

    • ^
    • v
    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.
    • ^
    • v
    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...
close Reblog this comment
blog comments powered by Disqus