Home » Scripting and Automation » Bash / Shell Scripts » How to extract only specific files from tar.bz2 / tar.gz in Linux ?

How to extract only specific files from tar.bz2 / tar.gz in Linux ?

The following shell script demos the creation of archive / tar and then use commands to check what are the contents of the tar without extracting the complete tar and then using command to extract only few selected files from the tar.

For Creating Tar / Archive, we used below tar command with argument “cvjf”,

$ tar cvjf tar1.tar.bz2 tar1/

For Listing the contents of tar without extracting complete tar, you can use below command,

$ tar -tf tarwhole.tar.bz2

For Extracting only selected files from tar, use below command.

TAR1=`tar -tf tarwhole.tar.bz2 | grep tar1`
tar xf tarwhole.tar.bz2 $TAR1

The complete shell script will be as following.

#!/bin/sh
#create 3 temporary folders which we will have actual contents like files/source codes etc.
mkdir tar1
mkdir tar2
mkdir tarwhole

#create a test file in first directory which we will zip/tar to some name.
cd tar1/
touch this_is_tar1.txt

#create second file in second directory from which we will create a tar
cd ..

cd tar2/
touch this_is_tar2.txt

# Now lets create two different tars of this two seperate directories as,

cd ..

tar cvjf tar1.tar.bz2 tar1/
tar cvjf tar2.tar.bz2 tar2/

# lets move this two tars into another folder, which we will archive/tar into final tar as,
mv tar1.tar.bz2 tar2.tar.bz2 tarwhole/

# lets now create a final tar as,
tar cvjf tarwhole.tar.bz2 tarwhole

#cleanup all the unwanted directories now, since we already have those archived in to final tar,
rm -rf tar1 tar2 tarwhole

echo "check what are the files inside the whole tar"
tar -tf tarwhole.tar.bz2

echo "extracting first tar from whole tar"
TAR1=`tar -tf tarwhole.tar.bz2 | grep tar1`
tar xf tarwhole.tar.bz2 $TAR1
echo "this is tar1 $TAR1"

echo "extracting file from first tar which is inside whole tar"
TAR1_FILE=`tar -tf $TAR1 | grep this_is_tar1.txt`
tar xf $TAR1 $TAR1_FILE
echo "this is tar1 file $TAR1_FILE"

Subscribe our Rurban Life YouTube Channel.. "Rural Life, Urban LifeStyle"

Leave a Comment