Tuesday, February 21, 2017

Tar Command Examples

1) Creating tar files

    a) How to Create .tar file
   
        Below command creates uncompressed archive file.
     
        tar -cvf log.tar log




Options Meaning:

c – Creates a new .tar archive file.
v – Verbosely show the .tar file progress.
f – File name type of the archive file.


    b) How to Create .tar.gz OR .tgz files

        Below command creates compressed gzip archive file.Use z option

         tar zcvf logs.tar.gz logs/
   
    OR

    tar zcvf logs.tgz logs/
     





    c) How to create  tar.bz2 OR tbz OR tb2

        The bz2 feature compress and create archive file less than the size of the gzip 
        To create highly compressed tar file we use option as j.

        Below command is used for creating compressed archive file.

   tar jcvf logs.tar.bz2 logs/

   OR

   tar jcvf logs.tbz logs/

   OR

   tar jcvf logs.tb2 logs/




 

2) Extracting (untar) an archive using tar command


    a) Untar tar archive file using tar command
     
        x - Option is used for extract
     
        If you want untar in a different directory then use option as -C (specified directory).
     
     tar -xvf logs.tar 

   tar -xvf logs.tar -C /tmp/logs



      b) Extract tar.gz Archive File

           To Uncompress tar.gz archive file, just run following command. If would like to untar in                      different directory just use option -C and the path of the directory

     tar -xvf logs.tar.gz

     OR

     tar -xvf logs.tar.gz -C /tmp/logs/




     
        c) Extract tar.bz2 Archive File

            Below command is used for extracting compressed archive file tar.bz2

     tar -xvf logs.tar.bz2

     OR

     tar -xvf logs.tar.bz2 -C /tmp/logs/



3) List Content of tar Archive File

 
            t - Option is used for listing content of tar files.

     tar -tvf logs.tar

     tar -tvf logs.tar.gz

     tar -tvf logs.tar.bz2

4) Extract Single file from tar File


       a) Extract Single file from .tar File

           tar -xvf logs.tar logs/AdminServer.log



      b) Extract Single file from tar.gz File

          tar -zxvf logs.tar.gz logs/AdminServer.log



      c) Extract Single file from tar.bz2 File

          tar -jxvf logs.tar.bz2 logs/AdminServer.log




5) Extract Multiple files from tar, tar.gz and tar.bz2 File


  tar -xvf logs.tar logs/access.log logs/AdminServer.log

  tar -zxvf logs.tar.gz logs/access.log logs/AdminServer.log

  tar -jxvf logs.tar.bz2 logs/access.log logs/AdminServer.log

6)  Extract Group of Files using Wildcard


  tar -xvf logs.tar  logs/AdminServer.*

  tar -zxvf logs.tar.gz  logs/AdminServer.*

  tar -jxvf logs.tar.bz2  logs/AdminServer.*

  Note:- you can use this way also 
        tar -xvf logs.tar --wildcards *.xml

7)  Add Files or Directories to tar, tar.gz And tar.bz2 Files


      tar -rvf logs.tar ../../bin/setDomainEnv.sh

      Note: Only we can add files/directory to tar file


Tar Command Options:-

c – create a archive file.
x – extract a archive file.
v – show the progress of archive file.
f – filename of archive file.
t – viewing content of archive file.
j – filter archive through bzip2.
z – filter archive through gzip.
r – append or update files or directories to existing archive file.
W – Verify a archive file.
wildcards – Specify patters in unix tar command.

No comments:

Post a Comment