Unzip/Untar ALL of the .tar.gz files in a specified directory

Cute Girl Aug 5, 2008

There comes a time when you need to untar/unzip a LOT of files in a specified directory, especially after a huge downloading spree! =p

The Shell Script below will help Linux/Unix users to easily do so.

untar.sh

#!/bin/sh
##########################
#
# file: untar.sh
# description:
#
# This script is intended to run a batch process to untar/unzip
# ALL of the .tar.gz files in a specified directory.
#
#
##########################
#
# Usage Notes:
#
# The default 'untar.sh' command assumes that you want to untar/unzip ALL
# of the '.tar.gz' files from the current directory.
#
# You can also specify a directory...
# example: untar.sh /home/$LOGNAME/packages
#
#########################
if [ $1 ] && [ -d $1 ]; then
# use the specified directory
UNTAR_PATH=$1
else
# use current directory
UNTAR_PATH=$PWD
fi

if grep *.tar.gz: $UNTAR_PATH ; then
echo "Unzip'ing/Untar'ing all (.tar.gz) files in the specified directory ($UNTAR_PATH).";
find $UNTAR_PATH *.tar.gz -exec tar xzvf {} \;
else
echo "Sorry, there were no .tar.gz files found within the specified directory ($UNTAR_PATH).";
fi

Simply copy/paste the code above to your favorite text editor, save it as 'untar.sh', and then execute it as follows:

$ untar.sh /path/to/your/tar.gz/files

The command executes the untar.sh file, which should unzip/untar all of the files in the specified directory.

You may want to save (or create a symbolic link to) the untar.sh file in your /usr/bin directory, that way other users can easily share and execute the script, without it being stored in their directory.

Tagged with

(3) Comments

  1. Sunny Aug 27, 2008

    This simple online command works well for me..

    for i in *.tar.gz; do echo working on $i; tar xvzf $i ; done

    Sunny

  2. Guest Aug 27, 2008

    Dear sir,
    I'm Ritesh Jain,While going to home from office i lost my mobile nokia N 70 M Black colour mobile thefted by some body. have no other sources to help me except u!
    PLease help me,sir.
    Name : Ritesh Jain
    Address: Ritesh Jain K503, ASter Building Jalvayu Vihar Kharghar Navi Mumbai
    Phone Model : Nokia N 70 M
    Make : NOKIA
    Colour : Black
    E-mail :[email protected]
    Missed date : 27/08/2008
    IMEI No 351863017343126
    Please help me sir
    I never forget your help,

  3. Guest Apr 14, 2011

    Did a small modification to the script...created an batch_untar.sh script and ran it from the directory with all the .tar.gz files. works like a dream...

    for i in *.tar.gz;
    do echo working on $i;
    #tar -xvf - $i ;
    gzip -dc $i|tar -xvf -
    done

    Regards,
    Santosh.