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