#!/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|-q] 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." local quiet=false case $1 in --help) echo "$help" return 0 ;; -q) quiet=true shift ;; esac local x super restore_nullglob f cmd for x in "$@"; do case "$x" in # tars first, so they are higher pri than non-tar extensions *.tar ) $quiet || echo tar xf "$x" tar xf "$x" ;; *.tar.bz2 | *.tbz2 ) $quiet || echo tar xjf "$x" tar xjf "$x" ;; *.tar.gz | *.tgz ) $quiet || echo tar xzf "$x" tar xzf "$x" ;; *.tar.xz ) $quiet || echo tar Jxf "$x" tar Jxf "$x" ;; *.tar.zst ) $quiet || echo tar -I unzstd -xf "$x" tar -I unzstd -xf "$x" ;; *.zst ) $quiet || echo unzstd "$x" unzstd "$x" ;; *.xz) $quiet || echo pixz -d "$x" pixz -d "$x" ;; *.7z ) if type -p 7za &>/dev/null; then cmd=7za else cmd=7zr fi $quiet || echo 7zr x "$x" $cmd x "$x" ;; *.bz2 ) $quiet || echo bunzip2 "$x" bunzip2 "$x" ;; *.deb ) $quiet || echo ar x "$x" ar x "$x" restore_nullglob=false if ! shopt -q nullglob; then restore_nullglob=true fi tmp=(data.tar.* control.tar.*) if $restore_nullglob; then shopt -u nullglob fi if (( ${#tmp[@]} != 2 )); then echo "error: could not find data.tar.* or control.tar.*" >&2 return 1 fi for f in ${tmp[@]}; do case $f in *.xz) $quiet || echo tar xJf $f tar xJf $f rm -f $f ;; *.zst) $quiet || echo tar -I unzstd -xf $f tar -I unzstd -xf $f rm -f $f ;; *.gz) $quiet || echo tar xzf $f tar xzf $f rm -f $f ;; *) echo "ex: ERROR: file i didnt expect: $f" ;; esac done ;; *.dsc) $quiet || echo dpkg-source -x "$x" dpkg-source -x "$x" ;; *.gz ) $quiet || echo gunzip "$x" gunzip "$x" ;; *.iso ) local super if [[ $EUID != 0 ]]; then super=sudo fi local temp_dir temp_dir=$(mktemp -d) $super losetup -f $quiet || echo $super mount -o loop "$x" "$temp_dir" $super mount -o loop "$x" "$temp_dir" local dir="${x%%.iso}" mkdir "$dir" $quiet || cp -a "$temp_dir"/* "$dir" cp -a "$temp_dir"/* "$dir" $quiet || $super umount "$temp_dir" $super umount "$temp_dir" ;; *.jar) $quiet || echo jar xf "$x" jar xf "$x" ;; *.r[0-9][0-9]|*.rar ) $quiet || echo unrar x "$x" unrar x "$x" ;; *.rpm ) $quiet || echo "rpm2cpio $x | cpio --extract --make-directories" rpm2cpio "$x" | cpio --extract --make-directories ;; *.sfs) $quiet || echo $super unsquashfs "$x" $super unsquashfs "$x" ;; *.Z ) $quiet || echo uncompress "$x" uncompress "$x" ;; *.zip|*.xpi ) $quiet || echo unzip "$x" unzip "$x" ;; *) echo "I don't know how to extract $x";; esac done } ex "$@"