Tar and compress a file in one step

Posted September 15, 2003 by Quinn McHenry in 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.

 

About Quinn McHenry

Quinn was one of the original co-founders of Tech-Recipes. He is currently crafting iOS applications as a senior developer at Small Planet Digital in Brooklyn, New York.
View more articles by Quinn McHenry

The Conversation

Follow the reactions below and share your own thoughts.