Using tar archives

Document description: This document is designed to describe the basic ways of manipulating tar archives (also known as tarballs) by using the 'tar' command.

Document notes: This document comes to you with no warranties, and by following the examples below you accept that you are using the commands at your own risk. The information below is related to the GNU version of tar. If you are using a version which differs from it, the information below might be incorrect.

Contents of this document:
1. Creation a tar archive.
2. Listing the contents of a tar archive.
3. Extracting the contents of a tar archive.
4. Usage of the verbose options.
5. Appending and updating contents of tarballs.
6. Deleting files from the contents of a tar archive.
7. Pocket Reference

The 'tar' application is an archiving program designed to store and extract files from an archive file known as a tarfile.

You can create a tarfile by executing the following command `tar -cf my_archive.tar file1 file2 dir1 file3 dir2`. This command will create an archive named my_archive.tar and will add directories dir1 and dir2, all files and directories under dir1 and dir2 and files file1, file2, file3. So if we have the following tree (see below) in our current directory and we execute the command, the archive my_archive.tar will include all files and directories.
.
./file1
./file2
./file3
./dir1
./dir1/file3
./dir1/file4
./dir1/file5
./dir1/file6
./dir2
./dir2/file6
./dir2/file7
./dir2/file8
./dir2/file9

If you want to see the contents of certain tar archive you should execute the following command `tar -tf archive.tar`. So if you want to list the contents of the archive you have created before you should just execute the command `tar -tf my_archive.tar`. The output of this command will be something like this:
[Arcopix@helios tar.tutorial]$ tar -tf my_archive.tar
file1
file2
file3
dir1/
dir1/file3
dir1/file4
dir1/file5
dir1/file6
dir2/
dir2/file6
dir2/file7
dir2/file8
dir2/file9

Extracting an archive simply means that the contents of the package is being copied onto your filesystem (hard drive). You can do this by using the command `tar -xf archive.tar`. So if you want to extract the contents of the archive we have created earlier, you should just execute the command `tar -xf my_archive.tar`. Here is an example:
[Arcopix@helios tar.tutorial]$ ls -hl
total 12K
-rw-rw-r-- 1 Arcopix Arcopix 10K Oct 9 01:13 my_archive.tar
[Arcopix@helios tar.tutorial]$ tar -xf my_archive.tar
[Arcopix@helios tar.tutorial]$ ls -hl
total 20K
drwxrwxr-x 2 Arcopix Arcopix 4.0K Oct 9 01:12 dir1
drwxrwxr-x 2 Arcopix Arcopix 4.0K Oct 9 01:12 dir2
-rw-rw-r-- 1 Arcopix Arcopix    2 Oct 9 01:12 file1
-rw-rw-r-- 1 Arcopix Arcopix    4 Oct 9 01:10 file2
-rw-rw-r-- 1 Arcopix Arcopix    6 Oct 9 01:10 file3
-rw-rw-r-- 1 Arcopix Arcopix  10K Oct 9 01:13 my_archive.tar

What is verbosity, and why you might want to use it. Verbosity means how much additional information you want to see. By default the tar application runs with no verbose messages. To add verbosity level just add a '-v' to the command either at the end of the command or in the operations area. For example `tar -tf my_archive.tar` with increased verbosity level can be executed in these ways `tar -tf my_archive.tar -v` or `tar -tvf my_archive.tar`. The both commands have no difference in execution. If you want to increase the verbosity level even more just add more 'v' like '-vvv' to the command

Verbose and creation - on the standard output you will see what files are being added to the archive. So if you execute `tar -cfv my_archive.tar file1 file2 dir1 file3 dir2`, the output will be like this one:
[Arcopix@helios tar.tutorial]$ tar -cvf my_archive.tar file1 file2 file3 dir1 dir2
file1
file2
file3
dir1/
dir1/file3
dir1/file4
dir1/file5
dir1/file6
dir2/
dir2/file6
dir2/file7
dir2/file8
dir2/file9
If you create an archive with higher verbosity you will this:
[Arcopix@helios tar.tutorial]$ tar -cvvf my_archive.tar file1 file2 file3 dir1 dir2
-rw-rw-r-- Arcopix/Arcopix 2 2007-10-09 01:12:06 file1
-rw-rw-r-- Arcopix/Arcopix 4 2007-10-09 01:10:54 file2
-rw-rw-r-- Arcopix/Arcopix 6 2007-10-09 01:10:55 file3
drwxrwxr-x Arcopix/Arcopix 0 2007-10-09 01:12:42 dir1/
-rw-rw-r-- Arcopix/Arcopix 2 2007-10-09 01:12:37 dir1/file3
-rw-rw-r-- Arcopix/Arcopix 4 2007-10-09 01:12:40 dir1/file4
-rw-rw-r-- Arcopix/Arcopix 6 2007-10-09 01:12:41 dir1/file5
-rw-rw-r-- Arcopix/Arcopix 8 2007-10-09 01:12:42 dir1/file6
drwxrwxr-x Arcopix/Arcopix 0 2007-10-09 01:12:54 dir2/
-rw-rw-r-- Arcopix/Arcopix 2 2007-10-09 01:12:50 dir2/file6
-rw-rw-r-- Arcopix/Arcopix 4 2007-10-09 01:12:52 dir2/file7
-rw-rw-r-- Arcopix/Arcopix 6 2007-10-09 01:12:53 dir2/file8
-rw-rw-r-- Arcopix/Arcopix 8 2007-10-09 01:12:54 dir2/file9

Verbose and listing contents. If you want to see more information for certain archive you should execute the listing command with higher verbosity level. The command `tar -tfv my_archive.tar` will output additional information like owner and group, attributes, date of creation and size of each file / directory.
[Arcopix@helios tar.tutorial]$ tar -tvf my_archive.tar
-rw-rw-r-- Arcopix/Arcopix 2 2007-10-09 01:12:06 file1
-rw-rw-r-- Arcopix/Arcopix 4 2007-10-09 01:10:54 file2
-rw-rw-r-- Arcopix/Arcopix 6 2007-10-09 01:10:55 file3
drwxrwxr-x Arcopix/Arcopix 0 2007-10-09 01:34:55 dir1/
-rw-rw-r-- Arcopix/Arcopix 2 2007-10-09 01:34:43 dir1/file3
-rw-rw-r-- Arcopix/Arcopix 4 2007-10-09 01:34:45 dir1/file4
-rw-rw-r-- Arcopix/Arcopix 6 2007-10-09 01:34:47 dir1/file5
-rw-rw-r-- Arcopix/Arcopix 8 2007-10-09 01:34:52 dir1/file6
drwxrwxr-x Arcopix/Arcopix 0 2007-10-09 01:12:54 dir2/
-rw-rw-r-- Arcopix/Arcopix 2 2007-10-09 01:12:50 dir2/file6
-rw-rw-r-- Arcopix/Arcopix 4 2007-10-09 01:12:52 dir2/file7
-rw-rw-r-- Arcopix/Arcopix 6 2007-10-09 01:12:53 dir2/file8
-rw-rw-r-- Arcopix/Arcopix 8 2007-10-09 01:12:54 dir2/file9

Extracting with higher verbose level will give you more information on what is currently being extracted from the file. Here is an example with single and double verbosity level:
[Arcopix@helios tar.tutorial]$ tar -xvf my_archive.tar
file1
file2
file3
dir1/
dir1/file3
dir1/file4
dir1/file5
dir1/file6
dir2/
dir2/file6
dir2/file7
dir2/file8
dir2/file9

[Arcopix@helios tar.tutorial]$ tar -xvvf my_archive.tar
drwxrwxr-x Arcopix/Arcopix 0 2007-10-09 01:34:55 dir1/
-rw-rw-r-- Arcopix/Arcopix 2 2007-10-09 01:34:43 dir1/file3
-rw-rw-r-- Arcopix/Arcopix 4 2007-10-09 01:34:45 dir1/file4
-rw-rw-r-- Arcopix/Arcopix 6 2007-10-09 01:34:47 dir1/file5
-rw-rw-r-- Arcopix/Arcopix 8 2007-10-09 01:34:52 dir1/file6
drwxrwxr-x Arcopix/Arcopix 0 2007-10-09 01:12:54 dir2/
-rw-rw-r-- Arcopix/Arcopix 2 2007-10-09 01:12:50 dir2/file6
-rw-rw-r-- Arcopix/Arcopix 4 2007-10-09 01:12:52 dir2/file7
-rw-rw-r-- Arcopix/Arcopix 6 2007-10-09 01:12:53 dir2/file8
-rw-rw-r-- Arcopix/Arcopix 0 2007-10-09 01:12:54 dir2/file9
-rw-rw-r-- Arcopix/Arcopix 2 2007-10-10 13:14:39 file1
-rw-rw-r-- Arcopix/Arcopix 4 2007-10-10 13:14:42 file2
-rw-rw-r-- Arcopix/Arcopix 6 2007-10-10 13:14:45 file3

Appending new files and / or directories to a tarballs is almost as easy as the creation of a tar archive. Therefore there is a catch. Let's take the archive we have created before. You can see its contents by typing `tar -tvf my_archive.tar` (see how to list tarball's contents here).
[Arcopix@helios tar.tutorial]$ tar -tvf my_archive.tar
drwxrwxr-x Arcopix/Arcopix 0 2007-10-09 01:34:55 dir1/
-rw-rw-r-- Arcopix/Arcopix 2 2007-10-09 01:34:43 dir1/file3
-rw-rw-r-- Arcopix/Arcopix 4 2007-10-09 01:34:45 dir1/file4
-rw-rw-r-- Arcopix/Arcopix 6 2007-10-09 01:34:47 dir1/file5
-rw-rw-r-- Arcopix/Arcopix 8 2007-10-09 01:34:52 dir1/file6
drwxrwxr-x Arcopix/Arcopix 0 2007-10-09 01:12:54 dir2/
-rw-rw-r-- Arcopix/Arcopix 2 2007-10-09 01:12:50 dir2/file6
-rw-rw-r-- Arcopix/Arcopix 4 2007-10-09 01:12:52 dir2/file7
-rw-rw-r-- Arcopix/Arcopix 6 2007-10-09 01:12:53 dir2/file8
-rw-rw-r-- Arcopix/Arcopix 0 2007-10-09 01:12:54 dir2/file9
-rw-rw-r-- Arcopix/Arcopix 2 2007-10-10 13:14:39 file1
-rw-rw-r-- Arcopix/Arcopix 4 2007-10-10 13:14:42 file2
-rw-rw-r-- Arcopix/Arcopix 6 2007-10-10 13:14:45 file3

We will add the following files and directories to 'my_archive.tar':
./dir3/subdir1/file1
./dir3/subdir1/file2
./dir3/subdir1/file3
./dir3/subdir1
./dir3/subdir2/file1
./dir3/subdir2/file2
./dir3/subdir2/file3
./dir3/subdir2
./dir3/subdir3/file1
./dir3/subdir3/file2
./dir3/subdir3/file3
./dir3/subdir3
./dir3/file1
./dir3/file2
./dir3/file3
./dir3

We will do this by executing the command: `tar -rvf my_archive.tar dir3`. Note that the 'v' in the operations area is for verbosity - we will see what files we will add.
[Arcopix@helios tar.tutorial]$ tar -rvf my_archive.tar dir3
dir3/
dir3/subdir1/
dir3/subdir1/file1
dir3/subdir1/file2
dir3/subdir1/file3
dir3/subdir2/
dir3/subdir2/file1
dir3/subdir2/file2
dir3/subdir2/file3
dir3/subdir3/
dir3/subdir3/file1
dir3/subdir3/file2
dir3/subdir3/file3
dir3/file1
dir3/file2
dir3/file3

Now the my_archive.tar has the following contents:
[Arcopix@helios tar.tutorial]$ tar -tvf my_archive.tar
-rw-rw-r-- Arcopix/Arcopix 2 2007-10-10 13:14:39 file1
-rw-rw-r-- Arcopix/Arcopix 4 2007-10-10 13:14:42 file2
-rw-rw-r-- Arcopix/Arcopix 6 2007-10-10 13:14:45 file3
drwxrwxr-x Arcopix/Arcopix 0 2007-10-09 01:34:55 dir1/
-rw-rw-r-- Arcopix/Arcopix 2 2007-10-09 01:34:43 dir1/file3
-rw-rw-r-- Arcopix/Arcopix 4 2007-10-09 01:34:45 dir1/file4
-rw-rw-r-- Arcopix/Arcopix 6 2007-10-09 01:34:47 dir1/file5
-rw-rw-r-- Arcopix/Arcopix 8 2007-10-09 01:34:52 dir1/file6
drwxrwxr-x Arcopix/Arcopix 0 2007-10-09 01:12:54 dir2/
-rw-rw-r-- Arcopix/Arcopix 2 2007-10-09 01:12:50 dir2/file6
-rw-rw-r-- Arcopix/Arcopix 4 2007-10-09 01:12:52 dir2/file7
-rw-rw-r-- Arcopix/Arcopix 6 2007-10-09 01:12:53 dir2/file8
-rw-rw-r-- Arcopix/Arcopix 8 2007-10-09 01:12:54 dir2/file9
drwxrwxr-x Arcopix/Arcopix 0 2007-10-10 13:20:44 dir3/
drwxrwxr-x Arcopix/Arcopix 0 2007-10-10 13:14:59 dir3/subdir1/
-rw-rw-r-- Arcopix/Arcopix 2 2007-10-10 13:14:59 dir3/subdir1/file1
-rw-rw-r-- Arcopix/Arcopix 4 2007-10-10 13:14:59 dir3/subdir1/file2
-rw-rw-r-- Arcopix/Arcopix 6 2007-10-10 13:14:59 dir3/subdir1/file3
drwxrwxr-x Arcopix/Arcopix 0 2007-10-10 13:15:00 dir3/subdir2/
-rw-rw-r-- Arcopix/Arcopix 2 2007-10-10 13:15:00 dir3/subdir2/file1
-rw-rw-r-- Arcopix/Arcopix 4 2007-10-10 13:15:00 dir3/subdir2/file2
-rw-rw-r-- Arcopix/Arcopix 6 2007-10-10 13:15:00 dir3/subdir2/file3
drwxrwxr-x Arcopix/Arcopix 0 2007-10-10 13:15:01 dir3/subdir3/
-rw-rw-r-- Arcopix/Arcopix 2 2007-10-10 13:15:01 dir3/subdir3/file1
-rw-rw-r-- Arcopix/Arcopix 4 2007-10-10 13:15:01 dir3/subdir3/file2
-rw-rw-r-- Arcopix/Arcopix 6 2007-10-10 13:15:01 dir3/subdir3/file3
-rw-rw-r-- Arcopix/Arcopix 2 2007-10-10 13:14:56 dir3/file1
-rw-rw-r-- Arcopix/Arcopix 4 2007-10-10 13:14:56 dir3/file2
-rw-rw-r-- Arcopix/Arcopix 6 2007-10-10 13:14:56 dir3/file3

So where is the catch. If the one or more of the files is already in the contents of the tarball it will be overwritten. In this way you can update the contents of a certain tar archive.

In order to delete one or more files from a certain tarball you should use the '--delete' operand. Now we will remove the dir1 and all the files in it from 'my_archive.tar'. We will use the following command: `tar --delete -f my_archive.tar dir1`
[Arcopix@helios tar.tutorial]$ tar --delete -f my_archive.tar dir1
And here is the contents of the tarball after the deletion:
[Arcopix@helios tar.tutorial]$ tar -tvf my_archive.tar
drwxrwxr-x Arcopix/Arcopix 0 2007-10-09 01:12:54 dir2/
-rw-rw-r-- Arcopix/Arcopix 2 2007-10-09 01:12:50 dir2/file6
-rw-rw-r-- Arcopix/Arcopix 4 2007-10-09 01:12:52 dir2/file7
-rw-rw-r-- Arcopix/Arcopix 6 2007-10-09 01:12:53 dir2/file8
-rw-rw-r-- Arcopix/Arcopix 8 2007-10-09 01:12:54 dir2/file9
drwxrwxr-x Arcopix/Arcopix 0 2007-10-10 13:20:44 dir3/
drwxrwxr-x Arcopix/Arcopix 0 2007-10-10 13:14:59 dir3/subdir1/
-rw-rw-r-- Arcopix/Arcopix 2 2007-10-10 13:14:59 dir3/subdir1/file1
-rw-rw-r-- Arcopix/Arcopix 4 2007-10-10 13:14:59 dir3/subdir1/file2
-rw-rw-r-- Arcopix/Arcopix 6 2007-10-10 13:14:59 dir3/subdir1/file3
drwxrwxr-x Arcopix/Arcopix 0 2007-10-10 13:15:00 dir3/subdir2/
-rw-rw-r-- Arcopix/Arcopix 2 2007-10-10 13:15:00 dir3/subdir2/file1
-rw-rw-r-- Arcopix/Arcopix 4 2007-10-10 13:15:00 dir3/subdir2/file2
-rw-rw-r-- Arcopix/Arcopix 6 2007-10-10 13:15:00 dir3/subdir2/file3
drwxrwxr-x Arcopix/Arcopix 0 2007-10-10 13:15:01 dir3/subdir3/
-rw-rw-r-- Arcopix/Arcopix 2 2007-10-10 13:15:01 dir3/subdir3/file1
-rw-rw-r-- Arcopix/Arcopix 4 2007-10-10 13:15:01 dir3/subdir3/file2
-rw-rw-r-- Arcopix/Arcopix 6 2007-10-10 13:15:01 dir3/subdir3/file3
-rw-rw-r-- Arcopix/Arcopix 2 2007-10-10 13:14:56 dir3/file1
-rw-rw-r-- Arcopix/Arcopix 4 2007-10-10 13:14:56 dir3/file2
-rw-rw-r-- Arcopix/Arcopix 6 2007-10-10 13:14:56 dir3/file3
drwxrwxr-x Arcopix/Arcopix 0 2007-10-09 01:12:54 dir2/
drwxrwxr-x Arcopix/Arcopix 0 2007-10-10 13:20:44 dir3/
drwxrwxr-x Arcopix/Arcopix 0 2007-10-10 13:14:59 dir3/subdir1/
drwxrwxr-x Arcopix/Arcopix 0 2007-10-10 13:15:00 dir3/subdir2/
drwxrwxr-x Arcopix/Arcopix 0 2007-10-10 13:15:01 dir3/subdir3/
-rw-rw-r-- Arcopix/Arcopix 2 2007-10-10 13:53:09 file1
-rw-rw-r-- Arcopix/Arcopix 4 2007-10-10 13:53:25 file2
-rw-rw-r-- Arcopix/Arcopix 6 2007-10-10 13:53:14 file3