Paste bdo94 Bash
#!/bin/sh main () { [ $# -lt 2 ] && { usage exit 1 } case "$1" in l) shift list "$@";; x) shift extract "$@";; c*) create "$@";; *) arrerr "Unknown option: $1";; esac } arrerr () { >&2 echo $(basename "$0"): $@ } extract () { for f in "$@"; do if [ ! -e "$f" ]; then arrerr "$f doesn\'t exist" else case "$f" in *.tar.gz| *.tgz) tar xvzf "$f" ;; *.gz) gunzip "$f" ;; *.tbz2| *.tar.bz2) tar xvjf "$f" ;; *.bz2) bunzip2 "$f" ;; *.tar.xz) tar xvJf "$f" ;; *.xz) unxz "$f" ;; *.7z) 7z x "$f" ;; *.cbr | *.rar) 7z x "$f" ;; *.cbz | *.jar | *.zip) unzip "$f" ;; *.deb) dpkg -x "$f" ;; *.dmg) hdiutil mount "$f" ;; *.ecm) wine unecm.exe "$f" ;; *.exe) cabextract "$f" ;; *.lzma) unlzma "$f" ;; *.pax) pax -r < "$f" ;; *.pax.Z) uncompress "$f" | pax -r ;; *.tar) tar xvf "$f" ;; *.Z) uncompress "$f" ;; *.tar.lrz) lrzuntar "$f" ;; *.lrz) lrunzip "$f" ;; *) arrerr "Unknown archive "$f", trying 7z..." 7z x "$f" ;; esac fi done } list () { for f in "$@"; do if [ ! -e "$f" ]; then arrerr "$f doesn\'t exist" else case "$f" in *.tar.gz | *.tgz) tar tvzf "$f" ;; *.tbz2 | *.tar.bz2) tar tvjf "$f" ;; *.tar.xz) tar tvJf "$f" ;; *.7z) 7z l "$f" ;; *.bz2) # bunzip2 misses list option 7z l "$f" ;; *.lrz) arrerr "Listing of lrz not supported by anything" ;; *.tar) tar tf "$f" ;; *.cbr | *.rar) 7z l "$f" ;; *.cbz | *.jar | *.zip) unzip -l "$f" ;; *.deb) dpkg -l "$f" ;; *.gz) gunzip -l "$f" ;; *.lzma) unlzma -l "$f" ;; *.tar) tar tvf "$f" ;; *.xz) unxz -l "$f" ;; *.Z) uncompress -l "$f" ;; *) arrerr "Unknown archive $f, trying 7z..." 7z -l "$f" ;; esac fi done } in_path () { command -v "$@" > /dev/null 2>&1 } dieifexists() { [ -f "$1" ] && { arrerr "Output file $f already exists" exit 1 } } create () { cmd="$1" fn="$2" f="$fn".tar if [ -d "$2" ]; then shift else [ -z "$3" ] && { arrerr "Not enough arguments" exit 1 } shift 2 fi case "$cmd" in cj) f="$f.bz2" dieifexists "$f" if in_path pbzip2; then tar cvf - "$@" -C . | pbzip2 -c > "$f" else tar cvfj "$f" "$@" -C . fi ;; cJ) f="$f.xz" dieifexists "$f" if in_path pxz; then tar cvf - "$@" -C . | pxz -T 0 -z "$f.xz" else tar cvfJ "$f" "$@" -C . fi ;; cz) f="$f.gz" dieifexists "$f" if in_path pigz; then tar cvf - "$@" -C . | pigz -c > "$f" else tar cvfz "$f" "$@" -C . fi ;; cl) # not using lrztar because it can only tar a single directory tar cfv "$f" "$@" lrzip "$f" rm -f "$f" ;; c7) 7z a "$fn".7z "$@" ;; cZ) zip -r "$fn".zip "$@" ;; c) create cJ "$@" ;; *) arrerr "Unknown format option $cmd" exit 1 ;; esac } usage () { >&2 echo "\ Usage: arr [l|x|c] ARCHIVES l: list content of archives x: extract archives c\$FORMAT: create archive in \$FORMAT Formats: j = .tar.bz2 J = .tar.xz z = .tar.gz Z = .zip 7 = .7z l = .tar.lrz Creates xz without format specifier Examples: Create: arr cj test testfile1 testfile2 -> test.tar.bz2 arr c testdir -> testdir.tar.xz List: arr l test.tar.bz2 -> print testfile1 testfile2 Extract: arr x test.tar.bz2 -> extract testfile1 testfile2" } main "$@"