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

#!/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"
Android Android Commands Android Java Applications Application Libraries Bash / Shell Scripts Bluetooth driver Build Frameworks Commands and Packages Core Kernel C Programs Development Environment Setup Documents / Books Errors & Failures File Systems Framebuffer / Display Driver git Go Language Programs Hardware Platforms Home Kernel & Device Drivers Kernel Booting and Porting Linux, OS Concepts and Networking Linux Device Drivers Linux Host, Ubuntu, SysAdmin Linux Kernel Linux Networking Middleware Libraries, HAL NDK / Middleware / HAL Network Driver OS Concepts PHP Procfs Filesystem Programming Languages RaspberryPi Scripting and Automation Search Engine Optimisation ( SEO ) Social Media Socurce Code Management ( SCM ) System Administration, Security Testing and Debugging Uncategorized Userspace Utilities Web design and development Wordpress Yocto / Bitbake / Openembedded

Leave a Reply