update README for gitorious move
[small-misc-bash] / ex-function
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 rar rpm tar xz zip & variations.
20 See source for exact file extensions."
21 if [[ $1 == --help ]]; then
22 echo "$help"
23 fi
24 local x super
25 for x in "$@"; do
26 case "$x" in
27 *.tar.bz2 | *.tbz2 ) tar xvjf "$x" ;;
28 *.tar.gz | *.tgz ) tar xvzf "$x" ;;
29 *.bz2 ) bunzip2 "$x" ;;
30 *.gz ) gunzip "$x" ;;
31 *.tar ) tar xvf "$x" ;;
32 *.zip ) unzip "$x" ;;
33 *.Z ) uncompress "$x" ;;
34 *.7z ) 7za x "$x" ;;
35 *.deb ) ar xv "$x" ;;
36 *.rpm ) rpm2cpio "$x" | cpio --extract --make-directories --verbose ;;
37 *.tar.xz ) tar Jxvf "$x" ;;
38 *.iso )
39 if [[ $EUID != 0 ]]; then
40 if [[ $(sudo -v) == Sorry* ]]; then
41 echo error: need superuser priveledge for iso file
42 return 1
43 fi
44 super=sudo
45 fi
46 local temp_dir=$(mktemp -d)
47 losetup -f
48 $super mount -o loop "$x" "$temp_dir"
49 local dir="${x%%.iso}"
50 mkdir "$dir"
51 cp -av "$temp_dir"/* "$dir"
52 $super umount "$temp_dir"
53 ;;
54 *.r[0-9][0-9]|*.rar ) unrar x "$x" ;;
55 *) echo "I don't kno how to extract $x";;
56 esac
57 done
58 }