#!/bin/bash # Copyright (C) 2014 Ian Kelling # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # http://www.apache.org/licenses/LICENSE-2.0 # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. ex() { local help="Usage: ex [--help] FILE... Extract each FILE according to its extension. 7z bz2 deb gz iso rar rpm tar xz zip & variations. See source for exact file extensions." if [[ $1 == --help ]]; then echo "$help" fi local x super for x in "$@"; do case "$x" in *.tar.bz2 | *.tbz2 ) tar xvjf "$x" ;; *.tar.gz | *.tgz ) tar xvzf "$x" ;; *.bz2 ) bunzip2 "$x" ;; *.gz ) gunzip "$x" ;; *.tar ) tar xvf "$x" ;; *.zip ) unzip "$x" ;; *.Z ) uncompress "$x" ;; *.7z ) 7za x "$x" ;; *.deb ) ar xv "$x" ;; *.rpm ) rpm2cpio "$x" | cpio --extract --make-directories --verbose ;; *.tar.xz ) tar Jxvf "$x" ;; *.iso ) if [[ $EUID != 0 ]]; then if [[ $(sudo -v) == Sorry* ]]; then echo error: need superuser priveledge for iso file return 1 fi super=sudo fi local temp_dir=$(mktemp -d) losetup -f $super mount -o loop "$x" "$temp_dir" local dir="${x%%.iso}" mkdir "$dir" cp -av "$temp_dir"/* "$dir" $super umount "$temp_dir" ;; *.r[0-9][0-9]|*.rar ) unrar x "$x" ;; *) echo "I don't kno how to extract $x";; esac done }