BASH – Smart Extract Utility
May 4th, 2011 @ 15:38:34 | BASH, Linux, Mac, Scripts, Technology
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | #! /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).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | # 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 } |


