The tar Command


The tar Command

If you spend much time using Unix systems, you're bound to run into tar files. The tar command is a convenient and commonly used tool for personal archiving and distribution of files. It's imperative that you are familiar with tar if you are going to study Unix beyond the level of this book. As mentioned before, tar stands for Tape Archive, although this is used more often for standard packaging. It was originally designed for tape backup, but today can be used with other storage media.

What is packaging? Well, let's say you needed to send someone a program you created. You would most likely not just have one file, but multiple files. You may want to create a single file that basically encompasses all the rest of the files or directories you are looking to package. The purpose of assembling a collection of files into one combined file is for easier archiving, storage, and sending condensed versions of data to others. When run by itself, it produces files with a .tar extension. When combined with gzip, for data compression, the resulting file extensions may be .tgz, .tar.gz, or .tar.Z as mentioned earlier.

The tar command in its simplest form either creates or unpacks archive files. When creating an archive, you provide tar with a filename for the archive and a list of files that you want to archive. The tar program will collect all the files you specify and put them into one single filethis is commonly called a tarball.

When unpacking archives, you provide tar with the name of a tar file and it extracts the contents of the file into the current directory, or a directory you specify with the exact same filenames, paths, and contents that existed on the system where it was tarred.

To use tar to create an archive, use the following steps:

1.

Select the data you want to archive and develop a name for the tar file itself. For example, since it's an archive, you may want to use dates along with an appropriate title so that you can see what kind of archive it is. This is not mandatory of course, just a suggestion.

2.

Issue the tar command as tar -cvf <tarfilename.tar> <files or directories>.

To picture this in use, consider having a directory named storage and you want to archive it. Everything in storage will be contained in the tar file that is created.

 > tar -cvf storage030105.tar storage 

In this example, I took the storage directory and made a tar file out of it called storage030105.tar. I used a date method so that I could keep track of what I was saving and when I saved it. In this example, we also see options being used (such as cvf), so let's cover what you can do with the tar command and its options.

There are many options you can use with the tar command. If you type tar -help, you will be amazed at how much you can do with the tar command. In this example, we used the cvf options.

The -c option specifies to Unix that your intentions are on creating a new archive. The -v (or verbose) option will show you all the files that are being packaged. The f option tells Unix whether to put this data in a file archive.

This new file contains the entire contents of your storage directory with all the directory structure and file attribute information intact. To unpack a tar file, you simply issue the command tar -xvf <tarfilename.tar>.

In this example, all the options are the same except for one. The x option is used here when extracting your files from the tar archive. This can be seen here:

 > tar -xvf storage030105.tar 

After a list of everything in the file is shown by the verbose option, you will have extracted this file (and all its contents) identically into the current directory you are in now. Sometimes you may want to view what is in a tar file before you extract it. You can do this by changing the options. Type tar -tvf <tarfile.tar> before unpacking it if you are not sure what may be in a file you are ready to extract. The -t option is the list option, and asks tar to tell you about the contents of the file rather than unpack it. This is helpful if you want to quickly see what is inside.



    SAMS Teach Yourself Unix in 10 Minutes
    Sams Teach Yourself Unix in 10 Minutes (2nd Edition)
    ISBN: 0672327643
    EAN: 2147483647
    Year: 2005
    Pages: 170

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net