-
Notifications
You must be signed in to change notification settings - Fork 0
/
.functions
executable file
·284 lines (245 loc) · 6.86 KB
/
.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
#!/bin/bash
# Create a new directory and enter it
function md() {
mkdir -p "$@" && cd "$@"
}
# find shorthand
function f() {
find . -name "$1" 2>&1 | grep -v 'Permission denied'
}
# Start an HTTP server from a directory, optionally specifying the port
function server() {
local port="${1:-8000}"
open "http://localhost:${port}/" &
# statik is good because it won't expose hidden folders/files by default.
# npm install -g statik
statik --port "$port" .
}
function extract_filename(){
FILENAME="${1%.*}"
echo "$FILENAME"
}
function tox264(){
FILENAME="${1%.*}"
avconv -i "$1" -c:v libx264 -crf 19 -preset veryslow -c:a copy "$FILENAME.mkv"
chmod 644 "$FILENAME.mkv"
}
# Copy w/ progress
cp_p () {
rsync -WavP --human-readable --progress "$1" "$2"
}
# get gzipped size
function gz() {
echo "orig size (bytes): "
cat "$1" | wc -c
echo "gzipped size (bytes): "
gzip -c "$1" | wc -c
}
# 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 -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"
}
# direct it all to /dev/null
function nullify() {
"$@" >/dev/null 2>&1
}
# Apply distorion filter using ffmpeg
function distorion_filter(){
SEARCH_STRING="*.*"
if [ -z "$1" ]
then
echo "Process all files"
else
echo "Process all files with the extension '$1'"
SEARCH_STRING="*.$1"
fi
find . -type f -iname $SEARCH_STRING | while read LINE; do
INPUT_FILE=$LINE
echo "Processing file '$INPUT_FILE'"
DIR=$(dirname "$INPUT_FILE")
EXTENSION=${INPUT_FILE##*.}
MIME_TYPE=`exiftool $INPUT_FILE | grep MIME | awk '{print $4}'`
OUTPUT_ARGS=""
if [[ $MIME_TYPE =~ "image/*" ]]
then
echo "Handle image"
FILE_TYPE=`exiftool $INPUT_FILE | grep "File Type Extension" | awk '{print $5}'`
echo $FILE_TYPE
OUTPUT_ARGS="-vcodec $FILE_TYPE"
fi
FILENAME="${INPUT_FILE%.*}"_distortion
OUTPUT_FILE="$FILENAME."$EXTENSION
ffmpeg -i $INPUT_FILE -vf "lenscorrection=cx=0.5:cy=0.5:k1=-0.227:k2=-0.022" -crf 17 -q:v 0 -map_metadata 0 $OUTPUT_FILE < /dev/null
touch -r $INPUT_FILE $OUTPUT_FILE
done
}
function disk-backup(){
target_name=$2.$(date +%Y-%m-%d_%H-%M-%S).img.gz
echo "Create backup of $1 to $target_name"
pipe="cat"
if hash pv 2>/dev/null; then
pipe=pv
fi
sudo dd if=$1 conv=sync,noerror bs=1M | $pipe | gzip -c > $target_name
echo "Backup created at $target_name"
}
function disk-restore(){
echo "Restore image $1 to $2"
pipe="cat"
if hash pv 2>/dev/null; then
pipe=pv
fi
sudo gunzip -c $1 | $pipe | sudo dd of=$2 conv=sync,noerror bs=1M
}
function speed_test_1gb(){
echo "Test write speed"
dd if=/dev/zero of=tempfile bs=1M count=1024 conv=fdatasync,notrunc status=progress
echo "Drop caches"
echo 3 | sudo tee /proc/sys/vm/drop_caches > /dev/null
echo "Test read speed uncached"
dd if=tempfile of=/dev/null bs=1M count=1024 status=progress
echo "Test read speed cached"
dd if=tempfile of=/dev/null bs=1M count=1024 status=progress
echo "Delete tmp file"
rm tempfile
}
function count_extensions () {
find "$1" -type f | grep -E ".*\.[a-zA-Z0-9]*$" | sed -e 's/.*\(\.[a-zA-Z0-9]*\)$/\1/' | sort | uniq -c | sort -nr
}
# Kill intellij prozesses
function kill_intellij () {
ps -ef | grep -i intellij | awk 'NR > 1 { print prev } { prev = $2 }' | xargs kill -9
}
function echo_server (){
if hash http-echo-server 2>/dev/null; then
if [ $# -eq 1 ]
then
export PORT=$1
fi
http-echo-server
else
echo "http-echo-server seems not be installed. You can install it using 'npm install http-echo-server -g'."
fi
}
# Fetch http status code description
function httpstatuscode(){
curl -s https://www.iana.org/assignments/http-status-codes/http-status-codes.txt | grep "$1" | head -1
}
function doWhile() {
while true; do eval $1; sleep $2; done
}
function tryWhile() {
until eval "$1"
do
sleep "$2"
done
}
function addAlias() {
echo "alias $1=$2" >> "$HOME/dotfiles/.aliases"
}
function decryptString() {
echo "$1" | openssl enc -aes-256-cbc -d -a -md sha256
}
function encryptString() {
echo "$1" | openssl enc -aes-256-cbc -a -md sha256
}
function removeXAttr() {
xattr "$1" | xargs -I{} zsh -c "xattr -d {} $1"
}
function showXAttr() {
xattr "$1" | xargs -I{} zsh -c "xattr -p {} $1"
}
function decode_base64_url() {
local len=$((${#1} % 4))
local result="$1"
if [ $len -eq 2 ]; then result="$1"'=='
elif [ $len -eq 3 ]; then result="$1"'='
fi
echo "$result" | tr '_-' '/+' | base64 --decode
}
function decode_jwt(){
decode_base64_url "$(echo -n "$1" | cut -d "." -f 1)" | jq .
decode_base64_url "$(echo -n "$1" | cut -d "." -f 2)" | jq .
}
function publicip() {
curl ifconfig.me
}
# Decode JWT
alias jwt="decode_jwt"
function generateRSAKeyPair(){
openssl genrsa -out privateKey.pem 4096
openssl rsa -in privateKey.pem -outform PEM -pubout -out publicKey.pem
}
function tar-backup(){
if [ "$#" -ne 2 ]; then
echo "Usage: tar-backup <output.tar.gz> <source-dir>"
return 1
fi
sudo tar -cvpzf "$1" "$2"
}
function tar-backup-extract(){
if [ "$#" -ne 2 ]; then
echo "Usage: tar-backup-extract <backup.tar.gz> <output-dir>"
return 1
fi
tar -xvpzf "$1" -C "$2" --numeric-owner
}
function clear-caches(){
yay -Sc --aur
go clean -modcache
sdk flush
npm cache clear --force
yarn cache clean
sudo docker system prune -a
sudo pacman -Scc
}