File: unpack-sh


 #!/bin/sh
 # Fri Jan  5 14:57:59 EST 2001
 # /usr/local/bin/unpack
 # Copyright 2001, 2002, Chris F.A. Johnson
 # Released under the terms of the GNU General Public License

 for file in "$@"
 do
 case $file in

  *.zip|*.ZIP)
     unzip "$file"
     ;;

  *.tar)
     tar xvpf "$file"
     ;;

 

Files with .tar.gz and .tgz suffixes are the same format: a tar archive compressed with gzip. The .Z suffix is used by compress. Gunzip can uncompress such files.

I use "gunzip -c" explicitly, because the z command is not included in all versions of tar.

*.tgz|*tar.gz|*.tar.Z)
	gunzip -c "$file" | tar xvp
	;;

	*.tar.bz2|*.tbz2)
	bunzip2 -c "$file" | tar xvp
	;;

	## gzipped and bzip2ed files are uncompressed to the current
	## directory, leaving the original files in place
	*.gz)
	gunzip -c "$file" > `basename "$file" .gz`
	;;

	*.Z)
	gunzip -c "$file" > `basename "$file" .Z`
	;;

	*.bz2)
	bunzip2  -c "$file" > `basename "$file" .bz2`
	;;

  *.rpm)
  dir="`basename "$file" .rpm`"
	mkdir -p "$dir" || die "Could not create $dir"
	(
		          cd "$dir" || die "Could not cd to $dir"
		          rpm2cpio "$file" | cpio -dim
	)
	;;

  *.deb)
  dir="`basename "$file" .deb`"
	mkdir -p "$dir" || die "Could not create $dir"
	(
		          cd "$dir" || die "Could not cd to $dir"
		          ar -x "$file"
		          [ -f control.tar.gz ] && gunzip -c  control.tar.gz | tar xvp
		          [ -f data.tar.gz ] && gunzip -c  data.tar.gz | tar xvp
	)
	;;
  esac
  done

This script appeared in the December 2003 issue of SysAdmin magazine and online at UnixReview.com.


Download: unpack