tar gzip example - How to work with files that are tar'd and gzip'd

tar gzip FAQ: How do I work with tar archives that have been created with tar and gzip?

When you work on Unix, Linux, and Mac OS X systems, you'll quickly find that tools like tar and gzip are your good friends, so learning how to work with them is very important. Here's a quick look at how to work with the most common tar/gzip scenarios.

1) Create a tar'd and gzip'd archive of a directory

A lot of times you'll have a directory that you want to either (a) make a backup copy of, or (b) share with other people. The most common way to do that these days is to create an archive that is "tar'd and gzip'd".

Here's how you create a tar'd and gzip'd archive of a directory (i.e., a "tar tgz" file) in your current folder named mydirectory:

tar czvf mydirectory.tgz mydirectory

A few quick notes about this tar/gzip example:

  1. I've included four options with the tar command:
    1. c - create a new archive.
    2. z - gzip'd the archive.
    3. v - work verbosely, showing me the name of each file you add. This is optional.
    4. f - specifies that you want to use the following filename (mydirectory.tgz) as the name of the archive.
  2. When creating an archive that has been tar'd and gzip'd, this is considered a "tar tgz" file, so it's common practice to end your filename with the extension tgz (though you can call it whatever you want).

2) Extract the contents of a tar/gzip (tar tgz) archive

Now imagine that you have just received a tar/gzip archive like this from someone else. To extract the contents of the archive issue a very similar command, this time using an x (for "eXtract") instead of the c argument, like this:

tar xzvf mydirectory.tgz

This command extracts whatever was in that tar'd and gzip'd archive to your filesystem.

3) List the contents of a tar/gzip archive

If that previous command sounded scary, maybe it should be. If this is an archive you just received from someone else, you may want to look at its contents before just extracting it to your filesystem.

Fortunately you can list the contents of an archive very easily, using the t option ("lisT") instead of the x argument, like this:

tar tzvf mydirectory.tgz

This command lists the contents of the archive, but does not extract the files in the archive to your filesystem.

4) How to gzip an existing tar file

As you work in the Unix world you'll also run into files named with a "tar.gz" extension, like this:

foo.tar.gz

When you see this ".tar.gz" file extension, it indicates that the file has probably been tar'd first, and then gzip'd second. This is a two-step process -- and the way I used to do this -- that works like this:

# step 1
tar cvf mydirectory.tar mydirectory

# step 2
gzip mydirectory.tar

As a practical matter you can treat a tar.gz file just like a tgz file, and extract its contents using the same command that was shown earlier:

tar xzvf mydirectory.tar.gz

Once I learned that I could tar and gzip a file with one command I stopped using this two-step process, but again, either way will work.

More tar and gzip tutorials

I hope this tar/gzip tutorial has been helpful. For more information on using the tar command, see my Linux tar command examples.

Add new comment

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.