Untitled Document
tar Command

tar command is an archival program that can create an acrchive for group of files or an entire directory structure .Lets run few examples to see how tar create the archive .

1. Basic Example -

The following command creates an archive of three files . To create an archive we need to specify the name of the archive ( with -f ) , the copy or write operations ( -c ) and the filenames as arguments . Additionally , we will use the -v ( verbose) option to display the progress while tar works . This is how we create archive.tar

   $ tar -cvf archive.tar emp.csv user.txt sample.txt                    
   a emp.csv 1285k
   a user.txt 130k
   a sample.txt 58k 

2. Recursive archiving to backup directories -

The following tar command archives the 2 directory structures shell_scripts and my_progs .

   $ tar -cvf shell_progs.tar shell_scripts my_progs                     

3. Compressing the archived files -

If the created archive is very big , you may like to compress it with gzip . Run the following command to compress the archive.tar file

   $ gzip archive.tar                      

4. Extracting files from archives ( -x ) option -

Ttar uses -x option to extract files from an archive . Lets extarct the files from archive.tar

   $ tar -xvf archive.tar                     

5. Extracting files zipped archived files -

Just now we have zipped archive.tar with gzip command . Now to exract files from this archive file , we have to first unzip it and then need to extract files .

   $ gunzip archive.tar.gz
   $ tar -xvf archive.tar                     

6. Extracting selected files or directories from archived files -

We can do selection on files and directories to be extracted from archived files . The following command extract my_progs directory from shell_progs.tar

   $ tar -xvf shell_progs.tar my_progs                     

7. Viewing the archived files ( -t ) archived files -

To view the contents of the archive , use -t ( table of contents ) option .It doesn't extract files , but simply shows their attributes in a form that you will see more often

   $ tar -tvf shell_progs.tar 
   -rw-r--r-- 108/12 3465 Mar 16 16:50 2016 my_progs
   -rwxr--r-- 110/12 40258798 Mar 20 07:50 2016 shell_scripts                

Linux Short Hand Commands For Archival -

8. Linux Options for Archival -

Linux tar and gzip are so often used together that GBU tar has -z option that compresses and archives together ( and decompresses and extracts together ) . This dual activity is reflected in the following commands :

    tar -cvzf shell_progs.tar.gz my_progs shell_scripts                         Archive and Compress the files 
    tar -xvzf shell_progs.tar.gz my_progs shell_scripts                         Decompress and Extract the files
    tar -xvzf shell_progs.tar.gz my_progs                                       Extracts One files my_progs
    tar -tvzf shell_progs.tar.gz                                                Display List              

Note that whether you should use the z option with -x or -t depends on whether the archive was compressed in the first place with -c . The archhive's extension (.tar.gz) should provide this hint , but that's not guarantee . A wrong extension could have been provide by the user at the time of archival .

Untitled Document