small doc update
[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 based on their extensions
19
20 7z bz2 deb gz iso dsc rar rpm tar xz zip sfs & some combinations.
21 See source for exact file extensions.
22 Note: apt-get install dtrx will do the same for
23 most of these types, plus some more, I'm going
24 to try it out sometime."
25 if [[ $1 == --help ]]; then
26 echo "$help"
27 fi
28 local x super
29 for x in "$@"; do
30 case "$x" in
31 *.tar.bz2 | *.tbz2 ) tar xjf "$x" ;;
32 *.tar.gz | *.tgz ) tar xzf "$x" ;;
33 *.bz2 ) bunzip2 "$x" ;;
34 *.gz ) gunzip "$x" ;;
35 *.tar ) tar xf "$x" ;;
36 *.zip ) unzip "$x" ;;
37 *.Z ) uncompress "$x" ;;
38 *.7z ) 7za x "$x" ;;
39 *.deb ) ar x "$x" ;;
40 *.rpm ) rpm2cpio "$x" | cpio --extract --make-directories ;;
41 *.tar.xz ) tar Jxf "$x" ;;
42 *.xz) xz -d "$x" ;;
43 *.iso )
44 local super
45 if [[ $EUID != 0 ]]; then
46 super=sudo
47 fi
48 local temp_dir=$(mktemp -d)
49 $super losetup -f
50 $super mount -o loop "$x" "$temp_dir"
51 local dir="${x%%.iso}"
52 mkdir "$dir"
53 cp -a "$temp_dir"/* "$dir"
54 $super umount "$temp_dir"
55 ;;
56 *.r[0-9][0-9]|*.rar ) unrar x "$x" ;;
57 *.dsc) dpkg-source -x "$x" ;;
58 *.sfs) $user unsquashfs "$x" ;;
59 *) echo "I don't kno how to extract $x";;
60 esac
61 done
62 }
63 ex "$@"