Murphy Mac » Page 'Tar'

Tar

tar
I’ve avoided the tar command for some time now, only to find there’s nothing to be afraid of.  My Windows background always led to pkzip for compression and archiving.  Knowing that zip shipped with OS X there wasn’t really a need to look any further.

But sometimes you’re sent things in the tar format.  Or the even more mysterious tar.gz.  If you have no experience with Linux or Unix the tar command and tar.gz files might leave you confused. Before OS X the only experience Murphy had with Linux was hacking a Directv TiVo so he could install a bigger drive.

Anyway - OS X makes tar and tar.gz files simple to work with.  Double-click one in the Finder and its contents are extracted.  But what if you want to create one?  Or see what’s inside before you open it?  We’ll do both of those things in the screencast, but here’s a quick overview of some tar basics.

tar -cvf irl08.tar Pictures/ireland_08

That command will create an archive called irl08.tar in the current directory.  All the files inside the ireland_08 directory will be placed inside, without compression.  The c creates the archive, the v displays progress as files are added, and the f specifies a file as the destination.  (tar stands for tape archive)

tar -zcvf irl08.tar.gz Pictures/ireland_08

This command is almost exactly the same, but the z option compresses the archive so we’ve added the customary gz to the file name.

Here’s how to view the contents of a tar archive:

tar -tvf irl08.tar

The t option lists the files.  If your archive is compressed add a z option too.

How does tar compare to zip?  Murphy isn’t sure what all the differences are.  He compressed a directory with dozens of screencasts in it using both utilities.  The resulting archive was about the same size for each.  The screencasts were already compressed themselves, so they might not be the best test subject.

A little research shows that extracting a single file from a very large archive might be quicker with zip than with tar.  A zip file includes a table of contents that makes locating an included file more direct.  And a zip file compresses the included files individually as they’re added.  A tar.gz file creates the archive first and then compresses the whole thing at once.  That can make single file extraction more time consuming as the entire large archive must be opened up.

There’s also gzip for compressing files.  But gzip deletes your original file, replacing it with the compressed one.   That makes Murphy a little nervous.  tar leaves your original files in place, so you can delete them if you need to.

tar.  One less thing to be afraid of.

You might find these posts interesting:

zip
command line encryption

Share/Save/Bookmark

Watch Now | Permalink

3 comments to “Tar”

  1. tar does exactly what its name suggests—’glues’ files together. It has no significant compression capabilities by itself, and its purpose, traditionally, was to make multiple files ‘as one’ for purposes of tape backups, network transfers etc. It’s gzip / bzip2 that bring in compression, but as you mentioned, already-compressed files aren’t the best for testing. See how they fare for source code ;)

  2. @inaequitas - Yes - I mentioned gzip in the post but defintely wasn’t clear that it handles the compression when you use the z option. Thanks.

  3. Murphy - try tar’ing >1 text files toegher, then look at the resulting tar with more or less. It will then be clear what tar does.

Leave a comment