-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbash_functions
More file actions
executable file
·329 lines (263 loc) · 9.2 KB
/
bash_functions
File metadata and controls
executable file
·329 lines (263 loc) · 9.2 KB
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
#!/bin/bash
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Clone a repository and install its dependencies.
clone() {
git clone "$1" \
|| return
cd "$(basename "${1%.*}")" \
|| return
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Check if there are dependencies to be installed.
if [ ! -f "package.json" ]; then
return
fi
# Check if the project uses Yarn.
if [ -f "yarn.lock" ] && command -v "yarn" $> /dev/null; then
printf "\n"
yarn install
return
fi
# If not, assume it uses npm.
if command -v "npm" $> /dev/null; then
printf "\n"
npm install
fi
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# 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.
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# From the specified files, rename the files containing a date
# in the filename to only the date in the following format:
#
# <year>-<month>-<day> <hour>.<minute>.<second>
#
# Usage examples:
#
# * rename-files-with-date-in-name path/to/some/directory path/to/some/file ...
rename-files-with-date-in-name() (
rename_file() (
filePath=$(dirname "${1%/}")
fileName=$(basename "$1")
# The following will do transformations such as:
#
# * 20200505_050505.dng => 2020-05-05 05.05.05.dng
# * 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
newFilePath="${filePath}/$(printf "%s" "$fileName" | sed 's/[^0-9]*\([0-9]\{4\}\)[_-]\{0,1\}\([0-9]\{2\}\)[_-]\{0,1\}\([0-9]\{2\}\)[_-]+\{0,1\}\([0-9]\{2\}\)[_-]\{0,1\}\([0-9]\{2\}\)[_-]\{0,1\}\([0-9]\{2\}\).*\(\..*\)$/\1-\2-\3 \4.\5.\6\7/')"
if [ "$newFilePath" != "$1" ]; then
printf "%s => %s\n" "$1" "$newFilePath"
mv -f "$1" "$newFilePath"
fi
)
# ┌─ Default to the current directory.
for filePath in "${@:-.}"; do
if [ -d "$filePath" ]; then
find "${filePath%/}" \
-type f \
-depth 1 \
-print \
| while read -r f; do
rename_file "$f"
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 convert command-line tool is installed.
if ! command -v "convert" $> /dev/null; then
printf "ImageMagick's 'convert' command-line tool is not installed!"
exit
fi
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
declare path="$(dirname "$1")"
declare fileName="$(basename "$1")"
declare geometry="${2:-50%}"
convert \
"$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"
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# 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.
}
# Create a .tar.gz archive, using `zopfli`, `pigz` or `gzip` for compression
function targz() {
local tmpFile="${*%/}.tar";
tar -cvf "${tmpFile}" --exclude=".DS_Store" "${@}" || return 1;
size=$(
stat -f"%z" "${tmpFile}" 2> /dev/null; # OS X `stat`
stat -c"%s" "${tmpFile}" 2> /dev/null # GNU `stat`
);
local cmd="";
if (( size < 52428800 )) && hash zopfli 2> /dev/null; then
# the .tar file is smaller than 50 MB and Zopfli is available; use it
cmd="zopfli";
else
if hash pigz 2> /dev/null; then
cmd="pigz";
else
cmd="gzip";
fi;
fi;
echo "Compressing .tar using \`${cmd}\`…";
"${cmd}" -v "${tmpFile}" || return 1;
[ -f "${tmpFile}" ] && rm "${tmpFile}";
echo "${tmpFile}.gz created successfully.";
}
# Make directory and enter it
function mkd() {
mkdir -p "$@" && cd "$@"
}
# whois a domain or a URL
function whois() {
local domain=$(echo "$1" | awk -F/ '{print $3}') # get domain from URL
if [ -z "$domain" ] ; then
domain=$1
fi
echo "Getting whois record for: $domain …"
# avoid recursion
# this is the best whois server
# strip extra fluff
/usr/bin/whois -h whois.internic.net "$domain" | sed '/NOTICE:/q'
}
# Extract archives - use: extract <file>
# Based on http://dotfiles.org/~pseup/.bashrc
function extract() {
if [ -f "$1" ] ; then
local filename=$(basename "$1")
local foldername="${filename%%.*}"
local fullpath=$(perl -e 'use Cwd "abs_path";print abs_path(shift)' "$1")
local didfolderexist=false
if [ -d "$foldername" ]; then
didfolderexist=true
read -r -p "$foldername already exists, do you want to overwrite it? (y/n) " -n 1
echo
if [[ $REPLY =~ ^[Nn]$ ]]; then
return
fi
fi
mkdir -p "$foldername" && cd "$foldername"
case $1 in
*.tar.bz2) tar xjf "$fullpath" ;;
*.tar.gz) tar xzf "$fullpath" ;;
*.tar.xz) tar Jxvf "$fullpath" ;;
*.tar.Z) tar xzf "$fullpath" ;;
*.tar) tar xf "$fullpath" ;;
*.taz) tar xzf "$fullpath" ;;
*.tb2) tar xjf "$fullpath" ;;
*.tbz) tar xjf "$fullpath" ;;
*.tbz2) tar xjf "$fullpath" ;;
*.tgz) tar xzf "$fullpath" ;;
*.txz) tar Jxvf "$fullpath" ;;
*.zip) unzip "$fullpath" ;;
*) echo "'$1' cannot be extracted via extract()" && cd .. && ! $didfolderexist && rm -r "$foldername" ;;
esac
else
echo "'$1' is not a valid file"
fi
}
# who is using the laptop's iSight camera?
camerausedby() {
echo "Checking to see who is using the iSight camera… 📷"
usedby=$(lsof | grep -w "AppleCamera\|USBVDC\|iSight" | awk '{printf $2"\n"}' | xargs ps)
echo -e "Recent camera uses:\n$usedby"
}
# backup files from a docker volume into /tmp/backup.tar.gz
# e.g. docker-volume-backup-compressed development_mysql_data
function docker-volume-backup-compressed() {
docker run -v "$1:/volume" -v /tmp:/backup --rm loomchild/volume-backup backup "$1_volume_backup"
}
# restore files from /tmp/backup.tar.gz into a docker volume
function docker-volume-restore-compressed() {
docker run -v "$1:/volume" -v /tmp:/backup --rm loomchild/volume-backup restore "$1_volume_backup"
}