improve documentation
[small-misc-bash] / ex
1 #!/bin/bash
2 # Copyright (C) 2014 Ian Kelling
3
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7
8 # http://www.apache.org/licenses/LICENSE-2.0
9
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 ex() {
17 local help="Usage: ex [--help] FILE...
18 Extract many types of files
19
20 Based on their extensions,
21 7z [tar.]bz2 deb [tar.]gz iso dsc rar rpm sfs tar xpi [tar.]xz zip Z.
22 See source for exact file extensions.
23 Note: dtrx (package & command) extracts most of these plus some others."
24
25 if [[ $1 == --help ]]; then
26 echo "$help"
27 fi
28 local x super
29 for x in "$@"; do
30 case "$x" in
31 *.7z ) 7za x "$x" ;;
32 *.bz2 ) bunzip2 "$x" ;;
33 *.deb ) ar x "$x" ;;
34 *.dsc) dpkg-source -x "$x" ;;
35 *.gz ) gunzip "$x" ;;
36 *.iso )
37 local super
38 if [[ $EUID != 0 ]]; then
39 super=sudo
40 fi
41 local temp_dir=$(mktemp -d)
42 $super losetup -f
43 $super mount -o loop "$x" "$temp_dir"
44 local dir="${x%%.iso}"
45 mkdir "$dir"
46 cp -a "$temp_dir"/* "$dir"
47 $super umount "$temp_dir"
48 ;;
49 *.jar) jar xf "$x" ;;
50 *.r[0-9][0-9]|*.rar ) unrar x "$x" ;;
51 *.rpm ) rpm2cpio "$x" | cpio --extract --make-directories ;;
52 *.sfs) $super unsquashfs "$x" ;;
53 *.tar ) tar xf "$x" ;;
54 *.tar.bz2 | *.tbz2 ) tar xjf "$x" ;;
55 *.tar.gz | *.tgz ) tar xzf "$x" ;;
56 *.tar.xz ) tar Jxf "$x" ;;
57 *.xz) xz -d "$x" ;;
58 *.Z ) uncompress "$x" ;;
59 *.zip|*.xpi ) unzip "$x" ;;
60
61 *) echo "I don't know how to extract $x";;
62 esac
63 done
64 }
65 ex "$@"