The 'tar' command.

The 'tar' command is designed to store and extract files from an archive.

The tar command works like this. You write 'tar' then you use one or more than one of the options, then you put the name of the archive and in the end the name of the files/directorys you want to archive.

This tutorial will cover the basics operations : create, list, append, extract.

For the sake of this tutorial, I will presume that you are located in a folder named /dog under your home directory (/bob/dog) wich has the following files in it : file1.txt and file2.txt.
Keep in mind that to create an archive you must have the permission to write in that directory.

1. How to create a tar archive.

a. Archiving files.

To create a tar archive you write :

tar -cvf archive.tar file1.txt file2.txt

You should have this output:

file1.txt
file2.txt

This command will create (-c) a tar archive in verbose mode (-v) that will be named (-f) 'archive.tar' wich will contain in it 'file1.txt' and 'file2.txt'.
The verbose mode will show on your screen exactly what the tar command shows in response. To make the exactly same archive, without the verbose mode you could write :

tar -cf archive.tar file1.txt file2.txt

but you will not see any output. Seeing the output often helps and gives a better understanding.

b. Archiving directories

Now you are located in /dog so to archive this we first must exit this directory (using the command 'cd ..') so now we are located in the /bob directory. Now we can archive the '/dog' directory writing :

tar -cvf dog.tar dog

You should have this output:

dog/
dog/file1.txt
dog/file2.txt
dog/archive.tar

2. How to list a tar archive.

To list the contens of the archive named 'dog.tar' you write:

tar -tvf dog.tar

You should have a similar output like this one:

drwxrwxr-x digitalgeek/digitalgeek 0 2002-12-26 15:16:17 dog/
-rw-rw-r-- digitalgeek/digitalgeek 9 2002-12-24 04:27:28 dog/file1.txt
-rw-rw-r-- digitalgeek/digitalgeek 10 2002-12-24 04:27:41 dog/file2.txt
-rw-rw-r-- digitalgeek/digitalgeek 10240 2002-12-26 15:16:17 dog/archive.tar

This command will list (-t) in verbose mode (-v) the contence of the tar file named (-f) 'dog.tar'. Using the verbose mode will show you a detailed explanation of the files/directories contained in the archive like permisions, owner, the date and the time when this files were created or were last modified.

3. How to append files to a tar archive.

We are now located in /bob where we have the archive named 'dog.tar'. Well to apend a file named 'file3.txt' that is located also in the /bob directory we
would write:

tar -rvf dog.tar file3.txt

You should have this output:

file3.txt

You can also append an entire directory to a archive instead of file3.txt we would write the name of the directory.

Now as you can allready guess, -r is used for append.

List the archive and you should have this output:

drwxrwxr-x digitalgeek/digitalgeek 0 2002-12-26 16:43:24 dog/
-rw-rw-r-- digitalgeek/digitalgeek 10240 2002-12-26 15:16:17 dog/archive.tar
-rw-rw-r-- digitalgeek/digitalgeek 9 2002-12-24 04:27:28 dog/file1.txt
-rw-rw-r-- digitalgeek/digitalgeek 10 2002-12-24 04:27:41 dog/file2.txt
-rw-rw-r-- digitalgeek/digitalgeek 11 2002-12-26 15:45:21 file3.txt

The append command is a little bit tricky. You can append a file to a tar archive even if it has the same name with a file that is allready in the archive. For example you archive a file, than you work on it more and then you use append and put it in the archive one more time. If you will list this archive you will see both files in there, but when you will extract this archive only the version stored last will wind up in the file system, because the newer files are extracted last so the old version wich is extrated first is overwritten by the newer version of the file.

4. How to extract files from a tar archive.

To extract the archive named dog.tar you write:

tar -xvf dog.tar

This will extract (-x) everything that is inside the archive inside the curent directory. Now ... the good part is that, if we had two files named file1.txt inside the archive, we can still extract the older version by writing :

tar -xkvf dog.tar

The -k stands for --keep-old-files wich will not let tar overwride the older version of the file (wich is extracted first) with the newer version. So the newer version is kept in the archive, and only the old version extracted.

To extract a specific file from the archive, for example file1.txt you would have to write :

tar -xvf dog.tar dog/file1.txt

Notice that you have to write the entire path to the file (dog/file1.txt).

to extract only the directory 'dog' without the file3.txt (witch was not in that directory and was appended later) you would write:

tar -xvf dog.tar dog

Notice that the 'tar' command has many other options. This tutorial was written to give a basic understanding. For better understanding read the manual : man tar