Shell script / commands to extract only certain files / folders from specific compressed tar file

#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"
Follow Lynxbee

Leave a Reply / Ask Question