Script: Extract

Published 05-04-2016 13:03:49

Raw

#!/bin/bash
Dir=$(pwd)
Say() { echo "$1" "$2" | lolcat ; notify-send "$1" "$2" ;}
MultipleFiles() {
	for f in $Dir/*
	do ExtractFile $(basename $f)
	done ;}
ExtractFile() {
	Name=${1%.*}
	mkdir $Name && cd $Name
	case $1 in
		*.tar.bz2|*.tbz2) Com="tar xvjf ../$1" ;;
		*.tar.gz|*.tgz) Com="tar xvzf ../$1" ;;
		*.tar.xz) Com="tar xvJf ../$1" ;;
		*.tar) Com="tar xvf ../$1" ;;
		*.lzma) Com="unlzma ../$1" ;;
		*.bz2) Com="bunzip2 ../$1" ;;
		*.rar) Com="unrar x -ad ../$1" ;;
		*.gz) Com="gunzip ../$1" ;;
		*.zip) Com="unzip ../$1" ;;
		*.Z) Com="uncompress ../$1" ;;
		*.7z) Com="7z x ../$1" ;;
		*.xz) Com="unxz ../$1" ;;
		*.exe) Com="cabextract ../$1" ;;
		*) Say "⛔ Unknown archive method" && cd $Dir && exit 1 ;;
	esac
	$Com && Say "👍 Extraction complete: $Name" || Say "⛔ Error extracting $Name"
	cd $Dir ;}

if [ $1 = "all" ]; then MultipleFiles
elif [ -f $1 ]; then ExtractFile $1
else Say "⛔ Error extracting $1"
fi