tar: Using the tar command to create an archive.

The tar command can be used to create a archive of multiple file and folders, making it easier to move or copy the files in one go.


Let us say we have three files, file1,file2,file3. We can create one tar file by combining all three using the command tar.

$ tar -cvf files.tar file1 file2 file3 file1 file2 file3 $ ls files.tar file1 file2 file3

The options mean :

c is to create
v is to make the command verbose, that is print what ever files are being archived
f is the use the file name being passed on the command line.

We can list the contents of the archive using. the “-t” option.

$ tar -tvf files.tar -rw-r–r– root/root 0 2012-07-15 21:37 file1 -rw-r–r– root/root 0 2012-07-15 21:37 file2 -rw-r–r– root/root 0 2012-07-15 21:37 file3

The option v and f mean the same as above.
The option “t” is to list or you can read it as tell the contents of the archive.
If we create a tar archive a of a directory, it maintains the folder structure intact, will produce the same folder structure when the archive is untared.

Example :

If we have a folder, temp , with three files, file1,file2,file3 and a sub folder temp1. We can create a tar archive of the same as follows.

$ ls temp -rw-r–r– 1 root root 0 Jul 15 21:37 file1 -rw-r–r– 1 root root 0 Jul 15 21:37 file2 -rw-r–r– 1 root root 0 Jul 15 21:37 file3 drwxr-xr-x 2 root root 4096 Jul 13 16:57 temp1 $ tar -cvf temp.tar temp temp/ temp/file2 temp/file3 temp/file1 temp/temp1/ $

Once an archive is created, if we update the folder which we archived we need to create a new archive but instead just update the already created archive.


In the previous example we created an archive for folder temp. If we add a new file,file4, to the folder we can update the archive a follows.

$ tar -uvf temp.tar temp test_tar/file4 $ tar -tvf temp.tar temp drwxr-xr-x root/root 0 2012-07-16 00:24 test_tar/ -rw-r–r– root/root 0 2012-07-15 21:37 test_tar/file2 -rw-r–r– root/root 0 2012-07-15 21:37 test_tar/file3 -rw-r–r– root/root 0 2012-07-15 21:37 test_tar/file1 drwxr-xr-x root/root 0 2012-07-13 16:57 test_tar/temp1/ -rw-r–r– root/root 0 2012-07-16 00:25 test_tar/file4

The tar command only updates the new file that was added to the folder.
If folder to be archived has symbolic links, then by default tar, copies the link only and not the original contents.


Let us create a soft link to the folder temp, used in the previous examples, and try to tar the link.

$ ln -s temp link $ ls -l link lrwxrwxrwx 1 root root 8 Jul 16 13:16 link -> test_tar $ tar -cvf link.tar link link $ tar -tvf link.tar lrwxrwxrwx root/root 0 2012-07-16 13:16 link -> test_tar

After creating the softlink, we used the tar command on the soft link to create the archive. And when we list the contents of the archive created, we can see it does not have the files and folders of the original file but only the link file.

To make sure that tar copies the original folder that the link is pointing to we need to pass the option “-h” , i.e.

$ tar -chvf link.tar link link/ link/file2 link/file3 link/file1 link/file4 link/temp1/ $ tar -tvf link.tar drwxr-xr-x root/root 0 2012-07-16 13:16 link/ -rw-r–r– root/root 0 2012-07-15 21:37 link/file2 -rw-r–r– root/root 0 2012-07-15 21:37 link/file3 -rw-r–r– root/root 0 2012-07-15 21:37 link/file1 -rw-r–r– root/root 0 2012-07-16 00:25 link/file4 drwxr-xr-x root/root 0 2012-07-13 16:57 link/temp1/

We can see that after passing the option “-h”, the archive contains all the original files and folders.
Once “tar” archive is created, we can extract the contents of the archive by using the option “-x”


To get the files archived under temp.tar in the above example we need to run

$ tar -xvf temp.tar temp/ temp/file2 temp/file3 temp/file1 temp/file4 temp/temp1/

The name of the folder created will the same as that of the original folder that was archived.

Tags: ,
Copyright 2017. All rights reserved.

Posted July 16, 2012 by Tux Think in category "Linux