add README
[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 if [[ $1 == --help ]]; then
20 echo "$help"
21 fi
22 local x
23 for x in "$@"; do
24 case "$x" in
25 *.tar.bz2 | *.tbz2 ) tar xvjf "$x" ;;
26 *.tar.gz | *.tgz ) tar xvzf "$x" ;;
27 *.bz2 ) bunzip2 "$x" ;;
28 *.gz ) gunzip "$x" ;;
29 *.tar ) tar xvf "$x" ;;
30 *.zip ) unzip "$x" ;;
31 *.Z ) uncompress "$x" ;;
32 *.7z ) 7za x "$x" ;;
33 *.deb ) ar xv "$x" ;;
34 *.rpm ) rpm2cpio "$x" | cpio --extract --make-directories --verbose ;;
35 *.tar.xz ) tar Jxvf "$x" ;;
36 *.iso )
37 local temp_dir=$(mktemp -d)
38 losetup -f
39 s mount -o loop "$x" "$temp_dir"
40 local dir="${x%%.iso}"
41 mkdir "$dir"
42 cp -av "$temp_dir"/* "$dir"
43 s umount "$temp_dir"
44 ;;
45 *.r[0-9][0-9]|*.rar ) unrar x "$x" ;;
46 *) echo "I don't kno how to extract $x";;
47 esac
48 done
49 }