Archiving Files (tar)


TAR is the name of both a useful program and a file type, both of which were created in UNIX. TAR was originally used to back up files to tape (thus giving it the name "Tape Archive"). It is now used with almost any operating system as a common format to begin file compression.


The TAR program gathers files together and creates an archive from the files without deleting the original files. This allows a person to transfer files from one computer and save it on another computer. This is useful when you want to copy a whole directory at once rather than individual files in the directory.


TAR format (also referred to as "Tarballs") is (are) commonly used to start file compression. Although this format does not automatically compress files (like .zip or .png), Tarballs does allow you to compress an entire group of files rather than an individual file.


Tar has commands that are used to tell it what to do.

  • c - creates a new tar file.
  • t - table of contents, see the names of all files or those specified in other command line arguments.
  • x - extract (restore) the contents of the tar file.
  • f - specifies where to store the tar file.
  • z - use zip/gzip to compress the tar file to make it smaller or to read from a compressed tar file.
  • v - verbose mode, tar will print what it's doing to the screen.
  • Only one out of the first three commands(c,t,and x) can be use with the last three commands(x,f,and z) at one time.


Tarring Files:

    tar cvzf name.tgz *.aa*.b
This will create(c), compress(z), name tar file(f) in this case name.tgz, and shows the files being stored into the tar file(v). For this example a tar file is created called name.tgz, to store all .aa and .b files.

The .tgz suffix is a convention for gzipped tar files(easy to use with z command).

Tarring a Directory:

    tar cvzf dir.tgz folder1

This will tar the directory folder1 including its sub-directories and files. A good thing about tarring a directory is when it's untarred it's untarred as a directory.

Viewing content of Tar file:

To view the table of content of a tar file use:

    tar tzf dir.tgz

Extracting Tar:

    tar xvzf dir.tgz

This command extract(x) the tar file to the current directory your in and then prints the files that are being extracted(v). To extract a tar file in a specified directory change the directory using the cd command. For example to extract into a directory called direct1:

    cd direct1
    and then use tar xvzf dir.tgz

To extract only one or several files you must know the name of the file that you want to extract. For example to extract ext.aa from folder1.tgz:

    tar xvzf folder1.tgz ext.aa