#!/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 many types of files Based on their extensions, 7z [tar.]bz2 deb [tar.]gz iso dsc rar rpm sfs tar xpi [tar.]xz zip Z. See source for exact file extensions. Note: dtrx (package & command) extracts most of these plus some others." if [[ $1 == --help ]]; then echo "$help" fi local x super for x in "$@"; do case "$x" in # tars first, so they are higher pri than non-tar extensions *.tar ) tar xf "$x" ;; *.tar.bz2 | *.tbz2 ) tar xjf "$x" ;; *.tar.gz | *.tgz ) tar xzf "$x" ;; *.tar.xz ) tar Jxf "$x" ;; *.xz) xz -d "$x" ;; *.7z ) 7za x "$x" ;; *.bz2 ) bunzip2 "$x" ;; *.deb ) ar x "$x" tmp=(data.tar.*) case $tmp in *.xz) tar xJf data.tar.xz ;; *.gz) tar xzf data.tar.gz ;; *) echo "error: could not find data.tar.*" >&2; exit 1 ;; esac tar xzf control.tar.gz rm -f control.tar.gz rm -f data.tar.* ;; *.dsc) dpkg-source -x "$x" ;; *.gz ) gunzip "$x" ;; *.iso ) local super if [[ $EUID != 0 ]]; then super=sudo fi local temp_dir=$(mktemp -d) $super losetup -f $super mount -o loop "$x" "$temp_dir" local dir="${x%%.iso}" mkdir "$dir" cp -a "$temp_dir"/* "$dir" $super umount "$temp_dir" ;; *.jar) jar xf "$x" ;; *.r[0-9][0-9]|*.rar ) unrar x "$x" ;; *.rpm ) rpm2cpio "$x" | cpio --extract --make-directories ;; *.sfs) $super unsquashfs "$x" ;; *.Z ) uncompress "$x" ;; *.zip|*.xpi ) unzip "$x" ;; *) echo "I don't know how to extract $x";; esac done } ex "$@"