9b82d786b8c8c3c477363f00d27bc90311847cc1
[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|-q] 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 local quiet=false
26 case $1 in
27 --help)
28 echo "$help"
29 return 0
30 ;;
31 -q)
32 quiet=true
33 shift
34 ;;
35 esac
36 local x super restore_nullglob f cmd
37 for x in "$@"; do
38 case "$x" in
39 # tars first, so they are higher pri than non-tar extensions
40 *.tar )
41 $quiet || echo tar xf "$x"
42 tar xf "$x"
43 ;;
44 *.tar.bz2 | *.tbz2 )
45 $quiet || echo tar xjf "$x"
46 tar xjf "$x"
47 ;;
48 *.tar.gz | *.tgz )
49 $quiet || echo tar xzf "$x"
50 tar xzf "$x"
51 ;;
52 *.tar.xz )
53 $quiet || echo tar Jxf "$x"
54 tar Jxf "$x"
55 ;;
56 *.tar.zst )
57 $quiet || echo tar -I unzstd -xf "$x"
58 tar -I unzstd -xf "$x"
59 ;;
60 *.zst )
61 $quiet || echo unzstd "$x"
62 unzstd "$x"
63 ;;
64 *.xz)
65 $quiet || echo pixz -d "$x"
66 pixz -d "$x"
67 ;;
68 *.7z )
69 if type -p 7za &>/dev/null; then
70 cmd=7za
71 else
72 cmd=7zr
73 fi
74 $quiet || echo 7zr x "$x"
75 $cmd x "$x"
76 ;;
77 *.bz2 )
78 $quiet || echo bunzip2 "$x"
79 bunzip2 "$x"
80 ;;
81 *.deb )
82 $quiet || echo ar x "$x"
83 ar x "$x"
84 restore_nullglob=false
85 if ! shopt -q nullglob; then
86 restore_nullglob=true
87 fi
88 tmp=(data.tar.* control.tar.*)
89 if $restore_nullglob; then
90 shopt -u nullglob
91 fi
92 if (( ${#tmp[@]} != 2 )); then
93 echo "error: could not find data.tar.* or control.tar.*" >&2
94 return 1
95 fi
96 for f in ${tmp[@]}; do
97 case $f in
98 *.xz)
99 $quiet || echo tar xJf $f
100 tar xJf $f
101 rm -f $f
102 ;;
103 *.zst)
104 $quiet || echo tar -I unzstd -xf $f
105 tar -I unzstd -xf $f
106 rm -f $f
107 ;;
108 *.gz)
109 $quiet || echo tar xzf $f
110 tar xzf $f
111 rm -f $f
112 ;;
113 *)
114 echo "ex: ERROR: file i didnt expect: $f"
115 ;;
116 esac
117 done
118 ;;
119 *.dsc)
120 $quiet || echo dpkg-source -x "$x"
121 dpkg-source -x "$x"
122 ;;
123 *.gz )
124 $quiet || echo gunzip "$x"
125 gunzip "$x"
126 ;;
127 *.iso )
128 local super
129 if [[ $EUID != 0 ]]; then
130 super=sudo
131 fi
132 local temp_dir
133 temp_dir=$(mktemp -d)
134 $super losetup -f
135 $quiet || echo $super mount -o loop "$x" "$temp_dir"
136 $super mount -o loop "$x" "$temp_dir"
137 local dir="${x%%.iso}"
138 mkdir "$dir"
139 $quiet || cp -a "$temp_dir"/* "$dir"
140 cp -a "$temp_dir"/* "$dir"
141 $quiet || $super umount "$temp_dir"
142 $super umount "$temp_dir"
143 ;;
144 *.jar)
145 $quiet || echo jar xf "$x"
146 jar xf "$x"
147 ;;
148 *.r[0-9][0-9]|*.rar )
149 $quiet || echo unrar x "$x"
150 unrar x "$x"
151 ;;
152 *.rpm )
153 $quiet || echo "rpm2cpio $x | cpio --extract --make-directories"
154 rpm2cpio "$x" | cpio --extract --make-directories
155 ;;
156 *.sfs)
157 $quiet || echo $super unsquashfs "$x"
158 $super unsquashfs "$x"
159 ;;
160 *.Z )
161 $quiet || echo uncompress "$x"
162 uncompress "$x"
163 ;;
164 *.zip|*.xpi )
165 $quiet || echo unzip "$x"
166 unzip "$x"
167 ;;
168
169 *) echo "I don't know how to extract $x";;
170 esac
171 done
172 }
173 ex "$@"