Seventh Sense Rambling about life's little things, in 7 ≡ 1 (mod 6) fashion



 «
  »


BASH – A smart extract utility

Why This Script?

As a systems administrator or as someone who dabbles in compilation technologies, it often becomes necessary to extract one or more variety of compressed files/folders. This script will automagically handle the options necessary for uncompressing different kinds of compressions.


The script


#! /bin/bash
#
# BASH script to automagically uncompress different kinds of compressed files/folders
#
# Usage: extract.sh [COMPRESSED_FILENAME]
# First written: Wed, 04 May 2011 05:14:10 -0400
# Last modified: Wed, 04 May 2011 05:14:10 -0400
#
 
E_BADARGS=65
 
if [ $# != 1 ]
then
  echo
  echo "  Usage: `basename $0` [COMPRESSED_FILENAME]"
  echo
  exit $E_BADARGS
else
  if [ -f $1 ]
  then
    case $1 in
      *.tar)       tar -xvf  $1  ;;
      *.tgz)       tar -xvzf $1  ;;
      *.tar.gz)    tar -xvzf $1  ;;
      *.tbz2)      tar -xvjf $1  ;;
      *.tar.bz2)   tar -xvjf $1  ;;
      *.gz)        gunzip    $1  ;;
      *.bz2)       bunzip2   $1  ;;
      *.zip)       unzip     $1  ;;
      *)           echo "  '$1' file type unknown" ;;
    esac
  else
    echo "  '$1' is not a regular file"
    echo
  fi
fi


Use it as a function!

Add the following lines to ${HOME}/.bashrc (and remember to source it).


# Smart extract function
extract () {
  if [ $# != 1 ]
  then
    echo
    echo "  Usage: extract [COMPRESSED_FILENAME]"
    echo
  else
    if [ -f $1 ]
    then
      case $1 in
        *.tar)       tar -xvf  $1  ;;
        *.tgz)       tar -xvzf $1  ;;
        *.tar.gz)    tar -xvzf $1  ;;
        *.tbz2)      tar -xvjf $1  ;;
        *.tar.bz2)   tar -xvjf $1  ;;
        *.gz)        gunzip    $1  ;;
        *.bz2)       bunzip2   $1  ;;
        *.zip)       unzip     $1  ;;
        *)           echo "  '$1' file type unknown" ;;
      esac
    else
      echo "  '$1' is not a regular file"
      echo
    fi
  fi
}




One response to “BASH – A smart extract utility

  1. Riley says:

    Nice litte script, g. This is certainly a handy time-saver when doing repetitive decompression with several different algorithms and file types.

Comments are closed.



Most of these posts, especially the ones with any hint of technical jargon, are intended to be Note2Self. But if any of them float your boat, then feel free to sail along.

If you feel so generous, improve my journey by sharing your thoughts!

Michigan Tech MS/PhD LaTeX Template


  @sgowtham

The Capitol: http://t.co/DvVSiqMnCR #Minnesota #StPaul #Architecture
about 15 hours ago


Minnehaha Falls: http://t.co/G4o9bdgOaJ #Minnesota #Nature
about 15 hours ago


@BrandonTHG Will do :)
2013.05.18 @ 1:50:25 pm


@BrandonTHG Eagan, Sir. Gathering with Mark, Linda & Gopher fans in a camp.
2013.05.18 @ 1:47:35 pm


Coffee on the house with a smile, again, in Settlers Co-op. Bruce Crossing continues to be classy, as it always has been to me!
2013.05.18 @ 1:31:00 pm