Tar and compress a file in one step

Contributor Icon Contributed by qmchenry  
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.

 

6 Comments -


  1. Satish said on March 11, 2009

    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

  2. Quinn McHenry said on March 11, 2009

    it would be:

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

  3. Anonymous said on August 17, 2009

    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

  4. Vinayak Kamath said on November 4, 2009

    You can use gtar instead

    gtar -cvzf target output.tgz

  5. GlobalNerds said on February 14, 2010

    How do you uncompress this in one step?

  6. Anonymous said on April 29, 2010

    To Compress !
    tar cjf /dir1 file1 /dir2 …. /dirN /fileN => For Bunzip

    tar czf /dir1 file1 /dir2 …. /dirN /fileN => For Gunzip

    You can always use
    “man tar” when you need help !

    To Extract/uncompress , replace the “c” with “x” in the above commands !
    Thanks ,
    Danny

 

RSS feed for comments on this post. TrackBack URL

Leave a comment -