forked from alrra/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bash_functions
144 lines (103 loc) Β· 3.83 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
#!/bin/bash
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Create data URI from a file.
datauri() {
local mimeType=""
if [ -f "$1" ]; then
mimeType=$(file -b --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")"
else
printf "%s is not a file.\n" "$1"
fi
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# 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
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Get gzip information (gzipped file size + reduction size).
gz() {
declare -i gzippedSize=0
declare -i originalSize=0
if [ -f "$1" ]; then
if [ -s "$1" ]; then
originalSize=$( wc -c < "$1" )
printf "\n original size: %12s\n" "$(hrfs "$originalSize")"
gzippedSize=$( gzip -c "$1" | wc -c )
printf " gzipped size: %12s\n" "$(hrfs "$gzippedSize")"
printf " βββββββββββββββββββββββββββββ\n"
printf " reduction: %12s [%s%%]\n\n" \
"$( hrfs $((originalSize - gzippedSize)) )" \
"$( printf "%s" "$originalSize $gzippedSize" | \
awk '{ printf "%.1f", 100 - $2 * 100 / $1 }' | \
sed -e "s/0*$//;s/\.$//" )"
# ββ remove tailing zeros
else
printf "\"%s\" is empty.\n" "$1"
fi
else
printf "\"%s\" is not a file.\n" "$1"
fi
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Human readable file size
# (because `du -h` doesn't cut it for me).
hrfs() {
printf "%s" "$1" |
awk '{
i = 1;
split("B KB MB GB TB PB EB ZB YB WTFB", v);
value = $1;
# confirm that the input is a number
if ( value + .0 == value ) {
while ( value >= 1024 ) {
value/=1024;
i++;
}
if ( value == int(value) ) {
printf "%d %s", value, v[i]
} else {
printf "%.1f %s", value, v[i]
}
}
}' |
sed -e ":l" \
-e "s/\([0-9]\)\([0-9]\{3\}\)/\1,\2/; t l"
# ββ add thousands separator
# (changes "1023.2 KB" to "1,023.2 KB")
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Create new directories and enter the first one.
mkd() {
if [ -n "$*" ]; then
mkdir -p "$@"
# ββ make parent directories if needed
cd "$@" \
|| exit 1
fi
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Search history.
qh() {
# ββ enable colors for pipe
# β ("--color=auto" enables colors only if
# β the output is in the terminal)
grep --color=always "$*" "$HISTFILE" | less -RX
# display ANSI color escape sequences in raw form βββ
# don't clear the screen after quitting less ββ
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Search for text within the current directory.
qt() {
grep -ir --color=always "$*" --exclude-dir=".git" --exclude-dir="node_modules" . | less -RX
# βββ search all files under each directory, recursively
# ββ ignore case
}