Unzip/Untar ALL of the .tar.gz files in a specified directory, using the 'find' command.

Category: Software and Systems Engineering
Author: Sean O'Donnell
Sat, Nov. 12th, 2005 @ 3:23:23 (MST)

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.
#
# Author: Sean O'Donnell http://www.seanodonnell.com/gpg/
#
##########################
#
# 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.

Copyleft (<) 1998-2019 www.seanodonnell.com