Monday, July 16, 2012
2:26 PM

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.

Example:

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



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.



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.



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.

Example:

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.



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.

Example:

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



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.



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"

Example:

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



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

0 comments:

Post a Comment