Thursday, 18 December 2014

Tar Files extraction Unix / Linux

Q: How can I extract  specific file from a tar ball ?

Tar or Tar ball is a single file  bundled with files &/ directories. First  we will discuss about general extraction of a files from tar ball.

Unpack or extract a tar file :

To unpack or extract a tar file, type:
tar -xvf myfile.tar
some times to save more space and bandwidth , we  need compress the tar balls using compression techniques like gzip or bzip2.

To unzip and extract  those tar files, type as below 
For  .tar.gz files 
tar -xzvf myfile.tar.gz

For .tar.bz2
tar -xjvf myfile.tar.bz2
-x : Extract a tar ball.
-v : Verbose output or show progress while extracting files.
-f : Specify an archive or a tarball filename.
-j : Decompress and extract the contents of the compressed archive created by bzip2 program (tar.bz2 extension).
-z : Decompress and extract the contents of the compressed archive created by gzip program (tar.gz extension).
Now here comes our main purpose ,  to extract a specific file  from a tar file.

Extract Specific file from a tar ball:

To extract a single file called myfile1.txt, enter:
tar -xvf file.tar myfile1.txt
tar -xzvf file.tar.gz myfile1.txt
tar -xjvf file.tar.bz2 myfile1.txt
You can also specify path such as home/um/myfile2.txt, enter:
tar -xvf file.tar home/um/myfile2.txt
tar -xzvf file.tar.gz home/um/myfile2.txt
tar -xjvf file.tar.bz2 home/um/myfile2.txt

How to Extract a Single Directory?

To extract a single directory called /home/um, enter:
tar -xvf file.tar home/um
tar -xzvf file.tar.gz home/um
tar -xjvf file.tar.bz2 home/um

Sample O/P:

0 blogger-disqus:

Post a Comment