forked from alrra/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbash_functions
More file actions
171 lines (122 loc) · 4.55 KB
/
bash_functions
File metadata and controls
171 lines (122 loc) · 4.55 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
#!/bin/bash
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Simple Calculator
? () {
local result=""
# ┌─ default (when --mathlib is used) is 20
result="$( printf "scale=10;%s\n" "$*" | bc --mathlib | tr -d "\\\n" )"
# remove the tailing "\" and "\n" ─┘
# (large numbers are printed on multiple lines)
if [[ "$result" == *.* ]]; then
# Improve the output for decimal numbers
printf "%s" "$result" |
sed -e "s/^\./0./" # add "0" for cases like ".5"` \
-e "s/^-\./-0./" # add "0" for cases like "-.5"`\
-e "s/0*$//;s/\.$//" # remove tailing zeros
else
printf "%s" "$result"
fi
printf "\n"
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# 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 %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 "$*" . | less -RX
# │└─ search all files under each directory, recursively
# └─ ignore case
}