How to use tar command in Unix



In continuation of our series of Unix tutorial ,i am here presenting the Unix Tutorial:  tar command example

tar – create tape archives and add or extract files.A more common use for tar is to simply combine a few files into a single file, for easy storage and distribution. The tar command archives and extracts files to and from a single file called a tarfile.

tar command example

-I include-file ==> Opens include-file containing a list of files, one per line, and treats it as if each file appeared separately on the command line.

Function Letters
The function portion of the key is specified by one of the
following letters:

c Create. Writing begins at the beginning of the tar-file, instead of at the end.
r Replace. The named files are written at the end of the tarfile.
t Table of Contents are listed.
u Update. The named files are written at the end of the tarfile if they are not already in the tarfile, or if
they have been modified since last written to that
tarfile.
x Extract or restore. The named files are extracted from
the tarfile and written to the directory specified in
the tarfile, relative to the current directory.
X Exclude. Use the exclude-file argument as a file con-
taining a list of relative path names for files (or
directories) to be excluded from the tarfile when
using the functions c, x, or t.

Practical tar command example

1. Using the tar Command to Create an Archive of Your Home Directory.

$tar -cvf user_dir.tar .

2. Lists the contents of the tar file created.

$tar -tvf user_dir.tar

3. To extract files from tar file:

$tar -xvf user_dir.tar

4. To tar files as per include options:

$tar -cvf user_dir.tar -I list_of_files

5. Extract a single file from tar

tar -xvf user_dir.tar /home/oracle/network/admin/tnsnames.ora

6.  Extract a single directory from tar

tar -xvf user_dir.tar /home/oracle/network/admin/

7.Extract group of files from tar  using regular expression

tar -xvf oracle_hom.tar –wildcards ‘*.ora’

8. Adding a file or directory to an existing archive using option -r

tar rvf host_dir.tar new.txt

tar rvf host_dir.tar new/


Few important things to note down for tar command line

1) The relative path is important while extracting file from tar

Example

tar -cvf user_dir.tar /home/oracle/jdk

It create a tar file of/home/oracle/jdk and its all inside directory in the current directly where the command is executed

Now if you plan to untar this file.
Then It will create file in the same relative path
i.e
/home/oracle/jdk/file1
/home/oracle/jdk/file2

Now if you take the tar backup like this

cd /home/oracle/

tar -cvf user_dir.tar jdk

It create a tar file of jdk and its all inside directory in the current directly where the command is executed
Now if you plan to untar this file.
Then It will create file in the same relative path
i.e
jdk/file1
jdk/file2

So basically you can create jdk directory in any folder by this approach while by first approach,it will always created in /home/oracle/

2) Compressing and un-compressing tar files
we can create a compressed tar file, via the following command:

tar -cvf – file1 file2 | gzip > file.tar.gz
tar -cvf – file1 file2 | compress > file.tar.Z

Uncompress will go like
gunzip file.tar.gz|tar xvf –
uncompress file.tar.Z|tar xvf –

Tar command in Linux distribution
The GNU tar command included with Linux distributions has integrated compression. It can create a .tar archive and then compress it with gzip or bzip2 compression in a single command.  The resulting file is a .tar.gz file or .tar.bz2 file.

It uses the extra letter z to achieve it

tar -czvf directory.tar.gz <directory Name>

One more option for compression is  available bzip2, it can be achieved using -j option

tar -cjvf directory.tar.bz2 <directory Name>

On some occasion you may wish to compress an entire directory, but not include certain files and directories. You can do so by appending an –exclude switch for each directory or file you want to exclude.

tar -czvf directory.tar.gz <directory Name>  –exclude=*.txt

Hope you like the post on tar command example /Linux tar command/Linux untar command

Related articles 

Useful Linux command for Oracle DBA

Awesome sed command in unix examples

Unix Script Tutorial : basic unix command

Writing loop statement in Unix shell scripting with examples


Leave a Reply