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.
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:
tar command:
c - create a new archive.z - gzip'd the archive.v - work verbosely, showing me the name of each file you add. This is optional.f - specifies that you want to use the following filename (mydirectory.tgz) as the name of the archive.tgz (though you can call it whatever you want).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.
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.
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.
I hope this tar/gzip tutorial has been helpful. For more information on using the tar command, see my Linux tar command examples.
Post new comment