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"
Android Android Commands Android Java Applications Application Libraries Application Stack / User Interface Bash / Shell Scripts Commands and Packages Compilation Content Management System - CMS Core Kernel C Programs Development & Build Development Environment Setup Errors & Failures Flutter git Go Language Programs Hardware Platforms HTML JAVA Programs Kernel & Device Drivers Linux, OS Concepts and Networking Linux Device Drivers Linux Host, Ubuntu, SysAdmin Linux Kernel Linux Networking Middleware Libraries, HAL Monetization / Google AdSense NDK / Middleware / HAL OS Concepts PHP Programming Languages RaspberryPi Scripting and Automation Search Engine Optimisation ( SEO ) Social Media Socurce Code Management ( SCM ) System Administration, Security Testing and Debugging Uncategorized User Interface Userspace Utilities Web design and development Wordpress Yocto / Bitbake / Openembedded

Leave a Reply / Ask Question