Just discovered a handy shortcut when working with the GNU utility “tar”. Like many other Unix utilities, the switches that you can pass tar change its behavior. To create a “plain” tar file (compressing multiple files down to a single tape archive format – similar to zip) you can execute the following:
bsimpson@Saturn:/tmp$ echo "test" > test bsimpson@Saturn:/tmp$ tar -cf test.tar test bsimpson@Saturn:/tmp$ file test* test: ASCII text test.tar: POSIX tar archive (GNU)
The “c” switch creates a new archive, and the “f” switch tells it that the name of the new archive follows. Similarly, we can untar this using the “x” switch, mutually exclusive with “c” for its create counterpart.
We can also apply compression to these new archives:
bsimpson@Saturn:/tmp$ echo "test" > test bsimpson@Saturn:/tmp$ tar -czf test.tar.gz test bsimpson@Saturn:/tmp$ file test* test: ASCII text test.tar.gz: gzip compressed data, from Unix, last modified: Tue Jan 11 22:31:34 2011
Or alternately, we can provide the bzip2 compression:
bsimpson@Saturn:/tmp$ echo "test" > test bsimpson@Saturn:/tmp$ tar -cjf test.tar.bz2 test bsimpson@Saturn:/tmp$ file test* test: ASCII text test.tar.bz2: bzip2 compressed data, block size = 900k
All well and good, however tar allows you to specify the compression (or lack thereof) based on the new filename. You call it what you want, and tar will figure out what compression to apply. Consider the following:
bsimpson@Saturn:/tmp$ echo "test" > test bsimpson@Saturn:/tmp$ tar -caf test.tar test bsimpson@Saturn:/tmp$ tar -caf test.tar.gz test bsimpson@Saturn:/tmp$ tar -caf test.tar.bz2 test bsimpson@Saturn:/tmp$ file test* test: ASCII text test.tar: POSIX tar archive (GNU) test.tar.bz2: bzip2 compressed data, block size = 900k test.tar.gz: gzip compressed data, from Unix, last modified: Tue Jan 11 22:24:43 2011
As a side note, these do not stack. For example “test.tar.gz.bz2” just produces a bzip2 encoded ASCII test file. If you *really* wanted this, you could use the pipe command to chain your compressions.
Hope this saves some time!
You should be aware that these are non-standard additions to `tar` (http://tinyurl.com/6y3wm8o). If you find yourself on a system that doesn’t support these switches then you might be at a loss. Personally, I never use them for creating compressed archives. I only use them when uncompressing an archive.
To do compression I do `tar cf – myfiles | bzip2 -9 – > myfiles.tar.bz2`. That’s completely portable, and you can use any compressor you like.
LikeLike