Unzip/Untar ALL Of The .tar.gz Files In A Specified Directory
Cute Girl • onIt & Systems 11 years ago • 2 min read

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.

Mumbai
Phone

Responses

Login to add comments on this post.

  • Guest 8 years ago

    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.