-
Notifications
You must be signed in to change notification settings - Fork 295
/
Copy pathbash_functions
executable file
Β·367 lines (282 loc) Β· 9.82 KB
/
bash_functions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#!/bin/bash
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Create data URI from a file.
datauri() {
local mimeType=""
if [ ! -f "$1" ]; then
printf "%s is not a file.\n" "$1"
return
fi
mimeType=$(file --brief --mime-type "$1")
# ββ do not prepend the filename to the output
if [[ $mimeType == text/* ]]; then
mimeType="$mimeType;charset=utf-8"
fi
printf "data:%s;base64,%s" \
"$mimeType" \
"$(openssl base64 -in "$1" | tr -d "\n")"
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Delete files that match a certain pattern from the current directory.
delete-files() {
local q="${1:-*.DS_Store}"
find . -type f -name "$q" -ls -delete
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Execute Vim macro
evm() {
local numberOfTimes="${*: -1}"
local files
if [[ "$numberOfTimes" =~ ^[0-9]+$ ]]; then
files=("${@:1:$#-1}")
else
numberOfTimes="1"
files=("$@")
fi
for file in "${files[@]}"; do
printf "* %s\n" "$file"
vim \
-c "norm! $numberOfTimes@q" \
-c "wq" \
"$file"
done
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Search history.
h() {
# ββ Enable colors for pipe.
# β ("--color=auto" enables colors only
# β if the output is in the terminal.)
grep --color=always "$*" "$HISTFILE" \
| less --no-init --raw-control-chars
# β ββ Display ANSI color escape sequences in raw form.
# ββ Don't clear the screen after quitting less.
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Optimize PDF files.
#
# Usage examples:
#
# optimize-pdfs path/to/some/directory path/to/some/file ...
optimize-pdfs() (
# Check if the pdfcpu command-line tool is installed.
if ! command -v "pdfcpu" &> /dev/null; then
printf "\n%s\n\n" "pdfcpu command-line tool is not installed!"
exit
fi
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
get-file-size() (
printf "%s" "$(wc -c < "$1")"
)
optimize-pdf() (
local filePath="$(dirname "${1%/}")"
local fileName="_$(printf "%s" "$(basename "$1")" | tr '[:upper:]' '[:lower:]')"
local optimizedFilePath="$filePath/$fileName"
printf "* %s\n" "$1"
pdfcpu optimize "$1" "$optimizedFilePath" &> /dev/null
local exitCode=$?
# If something went wrong (i.e. pdfcpu didn't create
# the optimized file or exited with a non-zero status)
# or the size of the optimized file is bigger than the
# original file, keep the original file.
if [ ! -f "$optimizedFilePath" ]; then
return
fi
if [ $exitCode -ne 0 ] || \
[ "$(get-file-size "$1")" -le "$(get-file-size "$optimizedFilePath")" ];
then
rm -rf "$optimizedFilePath"
return
fi
# Otherwise, replace the original file with the optimized one.
mv -f "$optimizedFilePath" "$1" 1> /dev/null
)
# ββ Default to the current directory.
for filePath in "${@:-.}"; do
if [ -d "$filePath" ]; then
find "${filePath%/}" \
-depth 1 \
-name \*.pdf \
-print \
-type f \
| while read -r file; do
optimize-pdf "$file"
done
elif [ -f "$filePath" ]; then
optimize-pdf "$filePath"
fi
done
)
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Rename media files.
#
# Rename the specified files so the filename is the file created date
# in the following format:
#
# <year>-<month>-<day> <hour>.<minute>.<second>
#
# Usage examples:
#
# rename-media-files path/to/some/directory path/to/some/file ...
rename-media-files() (
rename_file() (
filePath="$(dirname "${1%/}")"
fileName="$(printf "%s" "$(basename "$1")" | tr '[:upper:]' '[:lower:]')"
fileExtension="${fileName##*.}"
# iOS
#
# * IMG_1234.PNG => 2020-05-05 05.05.05.png
if [[ "$fileName" =~ ^img_[0-9]+ ]]; then
newFileName="$(stat -f %SB -t "%Y-%m-%d %H.%M.%S" "$1")"
# Handle the cases where multiple files have the same
# creation date.
index="$(find "$filePath" -maxdepth 1 -name "${newFileName}*" | wc -l | tr -d ' ')"
if [ "$index" -gt 0 ]; then
newFileName="$newFileName $index"
fi
mv "$1" "$filePath/$newFileName.$fileExtension"
return
fi
# Other
#
# Files that already contain the created date in the filename,
# but not in the intended format.
#
# * 20200505_050505.dng => 2020-05-05 05.05.05.dng
# * Screenshot 2020-01-02 at 03.04.05.png => 2020-01-02 03-04-05.jpg
# * Screenshot_20201010-101010_Something.jpg => 2020-10-10 10-10-10.jpg
# * signal-2020-05-06-07-08-09-123.mp4 => 2020-05-06 07-08-09.mp4
newFileName="$(printf "%s" "$fileName" | sed 's/[^0-9]*\([0-9]\{4\}\)[_-]\{0,1\}\([0-9]\{2\}\)[_-]\{0,1\}\([0-9]\{2\}\)[_-]\{0,1\}\( at \)\{0,1\}\([0-9]\{2\}\)[_.-]\{0,1\}\([0-9]\{2\}\)[_.-]\{0,1\}\([0-9]\{2\}\).*\(\..*\)$/\1-\2-\3 \5.\6.\7\8/')"
if [ "$newFileName" != "$fileName" ]; then
mv -f "$1" "$filePath/$newFileName"
fi
)
# ββ Default to the current directory.
for filePath in "${@:-.}"; do
if [ -d "$filePath" ]; then
find "${filePath%/}" \
-type f \
-depth 1 \
-print \
| while read -r file; do
rename_file "$file"
done
elif [ -f "$filePath" ]; then
rename_file "$filePath"
fi
done
)
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Resize image.
#
# Create a new image based on the specified image resized by the
# specified amount.
#
# $1: Path to the original image.
# $2: Resize value (default is '50%').
# See also: https://imagemagick.org/script/command-line-processing.php#geometry
#
# Usage examples:
#
# * resize-image ./path/to/image.jpg 30%
# * resize-image ./path/to/image.jpg 1000x1000!
resize-image() {
# Check if ImageMagick's `magick` command-line tool is installed.
if ! command -v "magick" &> /dev/null; then
printf "ImageMagick's 'magick' command-line tool is not installed!\n"
return
fi
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Check if the file is an image file.
if [ "$(file --mime-type --brief "$1" | cut -d'/' -f1)" != "image" ]; then
printf "'%s' is not an image file.\n" "$1"
return
fi
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
declare path="$(dirname "$1")"
declare fileName="$(basename "$1")"
declare geometry="${2:-50%}"
magick \
"$1" \
-colorspace RGB \
+sigmoidal-contrast 11.6933 \
-define filter:filter=Sinc \
-define filter:window=Jinc \
-define filter:lobes=3 \
-sigmoidal-contrast 11.6933 \
-colorspace sRGB \
-background transparent \
-gravity center \
-resize "$geometry" \
+append \
"$path/_$fileName" \
&& printf "* %s (%s)\n" \
"$path/_$fileName" \
"$geometry"
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Resize video.
#
# Create a new video based on the specified video resized by the
# specified amount.
#
# $1: Path to the original video file.
# $2: Resolution (default is '1920:1080').
# $3: Start time (default is '00:00:00').
# $4: Constant Rate Factor (default is '23').
#
# Usage examples:
#
# * resize-video ./path/to/video.mp4 "1080:1920" "00:00:01.500" "23"
resize-video() {
# Check if the `ffmpeg` command-line tool is installed.
if ! command -v "ffmpeg" &> /dev/null; then
printf "'ffmpeg' is not installed\n"
return
fi
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Check if the file is a video file.
if [ "$(file --mime-type --brief "$1" | cut -d'/' -f1)" != "video" ]; then
printf "'%s' is not a video file.\n" "$1"
return
fi
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
declare fileName="$(basename "$1")"
declare filePath="$(dirname "${1%/}")"
declare scale="${2:-1920:1080}"
declare startTime=${3:-00:00:00}
declare crf=${4:-23}
declare outputFileName="$filePath/_${fileName%.*}.mp4"
# The options used below try to offer a good balance between
# cross-platform interoperability, quality, and file size.
ffmpeg \
-i "$1" \
-ss "$startTime" \
-acodec aac \
-ac 2 \
-strict -2 \
-crf "$crf" \
-nostdin \
-preset veryslow \
-vcodec libx264 \
-vf "scale=${scale},setsar=1,format=yuv420p" \
-y \
"$outputFileName" \
&& printf "* %s / %s / %s\n" \
"$outputFileName" \
"$scale" \
"$startTime"
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Search for text within the current directory.
s() {
grep --color=always "$*" \
--exclude-dir=".git" \
--exclude-dir="node_modules" \
--ignore-case \
--recursive \
. \
| less --no-init --raw-control-chars
# β ββ Display ANSI color escape sequences in raw form.
# ββ Don't clear the screen after quitting less.
}