Like all other command line tools, tar is used from within a terminal window (such as aterm, gnome-terminal, or konsole). The basic format of the command is:
tar OPTIONS filename
The most useful options are:
c - This is used for creating an archive.
r - This allows you to add a file to the end of an archive.
t - This will list the contents of an archive.
x - This extracts an archive.
p - Preserve file permissions while extracting.
z - Filter the archive through gzip (for compression).
v - Verbosly list files that are processed.
f - Use the file following the options.
First we are going to look at unarchiving a file and then we will look at archiving a file. The example we will use is sourcefile.tgz.
To unpack the sourcefile.tgz archive use the following options:
x - to extract
v - to give verbose output
z - to run tar through gzip
f - to indicate the file we are working with follows the options
The z option must be used in the above case because, as you can see, there is a "z" in the file extension which indicates it is a compressed file. The final command will look like:
tar xvzf sourcefile.tgz
If the sourcefile.tgz archive has been packed correctly there will now be a newly created directory (within the directory tar was run in) called sourcefile.
Let's go the opposite direction now. You have sourcefile and you want to package it up for use. For this use the following options:
c - to create the new archive
f - to indicate the file to be used
z - to compress the file with gzip
The new command will look like:
tar cfz sourcefile.tgz sourcefile
Notice for archival creation you have to specify the name of the newly created archive. You can actually name this anything you like, but it is good practice to retain continuity.