add note about license
[small-misc-bash] / ex
1 #!/bin/bash
2 # I, Ian Kelling, follow the GNU license recommendations at
3 # https://www.gnu.org/licenses/license-recommendations.en.html. They
4 # recommend that small programs, < 300 lines, be licensed under the
5 # Apache License 2.0. This file contains or is part of one or more small
6 # programs. If a small program grows beyond 300 lines, I plan to switch
7 # its license to GPL.
8
9 # Copyright 2024 Ian Kelling
10
11 # Licensed under the Apache License, Version 2.0 (the "License");
12 # you may not use this file except in compliance with the License.
13 # You may obtain a copy of the License at
14
15 # http://www.apache.org/licenses/LICENSE-2.0
16
17 # Unless required by applicable law or agreed to in writing, software
18 # distributed under the License is distributed on an "AS IS" BASIS,
19 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 # See the License for the specific language governing permissions and
21 # limitations under the License.
22
23
24 ex() {
25 local help="Usage: ex [--help|-q] FILE...
26 Extract many types of files
27
28 Based on their extensions,
29 7z [tar.]bz2 deb [tar.]gz iso dsc rar rpm sfs tar xpi [tar.]xz zip Z.
30 See source for exact file extensions.
31 Note: dtrx (package & command) extracts most of these plus some others."
32
33 local quiet=false
34 case $1 in
35 --help)
36 echo "$help"
37 return 0
38 ;;
39 -q)
40 quiet=true
41 shift
42 ;;
43 esac
44 local x super restore_nullglob f cmd
45 for x in "$@"; do
46 case "$x" in
47 # tars first, so they are higher pri than non-tar extensions
48 *.tar )
49 $quiet || echo tar xf "$x"
50 tar xf "$x"
51 ;;
52 *.tar.bz2 | *.tbz2 )
53 $quiet || echo tar xjf "$x"
54 tar xjf "$x"
55 ;;
56 *.tar.gz | *.tgz )
57 $quiet || echo tar xzf "$x"
58 tar xzf "$x"
59 ;;
60 *.tar.xz )
61 $quiet || echo tar Jxf "$x"
62 tar Jxf "$x"
63 ;;
64 *.tar.zst )
65 $quiet || echo tar -I unzstd -xf "$x"
66 tar -I unzstd -xf "$x"
67 ;;
68 *.zst )
69 $quiet || echo unzstd "$x"
70 unzstd "$x"
71 ;;
72 *.xz)
73 $quiet || echo pixz -d "$x"
74 pixz -d "$x"
75 ;;
76 *.7z )
77 if type -p 7za &>/dev/null; then
78 cmd=7za
79 else
80 cmd=7zr
81 fi
82 $quiet || echo 7zr x "$x"
83 $cmd x "$x"
84 ;;
85 *.bz2 )
86 $quiet || echo bunzip2 "$x"
87 bunzip2 "$x"
88 ;;
89 *.deb )
90 $quiet || echo ar x "$x"
91 ar x "$x"
92 restore_nullglob=false
93 if ! shopt -q nullglob; then
94 restore_nullglob=true
95 fi
96 tmp=(data.tar.* control.tar.*)
97 if $restore_nullglob; then
98 shopt -u nullglob
99 fi
100 if (( ${#tmp[@]} != 2 )); then
101 echo "error: could not find data.tar.* or control.tar.*" >&2
102 return 1
103 fi
104 for f in ${tmp[@]}; do
105 case $f in
106 *.xz)
107 $quiet || echo tar xJf $f
108 tar xJf $f
109 rm -f $f
110 ;;
111 *.zst)
112 $quiet || echo tar -I unzstd -xf $f
113 tar -I unzstd -xf $f
114 rm -f $f
115 ;;
116 *.gz)
117 $quiet || echo tar xzf $f
118 tar xzf $f
119 rm -f $f
120 ;;
121 *)
122 echo "ex: ERROR: file i didnt expect: $f"
123 ;;
124 esac
125 done
126 ;;
127 *.dsc)
128 $quiet || echo dpkg-source -x "$x"
129 dpkg-source -x "$x"
130 ;;
131 *.gz )
132 $quiet || echo gunzip "$x"
133 gunzip "$x"
134 ;;
135 *.iso )
136 local super
137 if [[ $EUID != 0 ]]; then
138 super=sudo
139 fi
140 local temp_dir
141 temp_dir=$(mktemp -d)
142 $super losetup -f
143 $quiet || echo $super mount -o loop "$x" "$temp_dir"
144 $super mount -o loop "$x" "$temp_dir"
145 local dir="${x%%.iso}"
146 mkdir "$dir"
147 $quiet || cp -a "$temp_dir"/* "$dir"
148 cp -a "$temp_dir"/* "$dir"
149 $quiet || $super umount "$temp_dir"
150 $super umount "$temp_dir"
151 ;;
152 *.jar)
153 $quiet || echo jar xf "$x"
154 jar xf "$x"
155 ;;
156 *.r[0-9][0-9]|*.rar )
157 $quiet || echo unrar x "$x"
158 unrar x "$x"
159 ;;
160 *.rpm )
161 $quiet || echo "rpm2cpio $x | cpio --extract --make-directories"
162 rpm2cpio "$x" | cpio --extract --make-directories
163 ;;
164 *.sfs)
165 $quiet || echo $super unsquashfs "$x"
166 $super unsquashfs "$x"
167 ;;
168 *.Z )
169 $quiet || echo uncompress "$x"
170 uncompress "$x"
171 ;;
172 *.zip|*.xpi )
173 $quiet || echo unzip "$x"
174 unzip "$x"
175 ;;
176
177 *) echo "I don't know how to extract $x";;
178 esac
179 done
180 }
181 ex "$@"