-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbash_aliases
484 lines (390 loc) · 10.4 KB
/
bash_aliases
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
# Colorized prompt
if [ $UID != 0 ] ; then
PS1="\[\e[32;1m\]\u@\[\e[31;1m\]\h\[\033[00m\]:\[\033[01;33m\]\w\[\033[00m\]\\$ "
else
PS1="\[\e[31;1m\]\u@\[\e[31;1m\]\h\[\033[00m\]:\[\033[01;33m\]\w\[\033[00m\]\\$ "
fi
# append each command to the history
PROMPT_COMMAND="history -a; history -n"
# Colorized grep
alias grep='grep --color=auto'
export GREP_COLORS='mt=1;32:ne'
alias nocolor='sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[mGK]//g ; s/\x0f//g"'
alias nogarbage='tr -cd "[:print:]\n"'
alias lc='tr A-Z a-z'
alias uc='tr a-z A-Z'
alias quote4sql='sed -e "s/^/'\''/; s/$/'\'',/"'
trn() {
tr "$@" '\n'
}
LESS_VERSION="$(less --version | sed -n -e 's/less \([0-9]\+\) .*/\1/p')"
LESS_F=$( [ "$LESS_VERSION" -ge 530 ] && echo "less --quit-if-one-screen" || echo "less")
unalias l
l() {
if [ -t 1 ] ; then
$LESS_F -R "$@"
else
nocolor
fi
}
g() {
`which grep` --color=always "$@" | l
}
gr() {
g -r "$@"
}
GRE_EXCLUDE='--exclude *.pyc --exclude *.min.js --exclude *.min.css --exclude *.css.map --exclude *.log'
GRE_EXCLUDE_DIR='--exclude-dir venv --exclude-dir node_modules --exclude-dir gems --exclude-dir .svn --exclude-dir .git'
gre() {
# Need "set -f" to disable glob expansion for the "*.pyc" and other exclude patterns.
# But we do need the word splitting though, this is why simple quoting "$GRE_EXCLUDE" won't work.
# And you still can do the "gre *.txt" because the glob expansions happens earlier.
(set -f; g -r $GRE_EXCLUDE $GRE_EXCLUDE_DIR "$@")
}
grepy() {
(set -f; g -r $GRE_EXCLUDE_DIR --include '*.py' "$@")
}
x0g() {
xargs -0 `which grep` --color=always "$@" | l
}
grhw() {
where=$1; shift
find "$where" -maxdepth 3 -name '.bash_history*' -print0 | xargs -0 `which grep` -h --color=always "$@" | sort | uniq -c | sort -n -r | l
}
grh() {
grhw "$HOME" "$@"
}
grhs() {
find "$SCREEN_DIR" -maxdepth 1 -name '.bash_history*' -print0 | xargs -0 `which grep` --color=always "$@"
}
d() {
# The 'diff' command fails when run by mistake without arguments but 'colordiff' does not.
# This is understandable (colordiff can color the stdin) but annoying, so we'll fail explicitly here.
if [ -z "$1" ] ; then
echo "d: missing operands"
return 1
fi
colordiff "$@" | l
}
wd() {
wdiff -n "$@" | colordiff | l
}
j() {
jq --color-output "$@" | l
}
# Usage:
# diffcmd one two -u -- jq .
# diffcmd one two -u -- sort
# diffcmd one two -- sed -e 's/#.*//'
# diffcmd dir1 dir2 -- find {} -type f -printf "%P\n"
diffcmd() {
local diffargs
local extcmd
local extcmdargs
local left
local right
left="$1" ; shift
right="$1" ; shift
while [[ $1 != '--' && $1 != '' ]]; do
diffargs+=("$1")
shift
done
shift # skip the "--"
while [[ $1 != '{}' && $1 != '' ]]; do
extcmd+=("$1")
shift
done
shift # skip the "{}"
extcmdargs=("$@") # suffix args
d "${diffargs[@]}" <("${extcmd[@]}" "$left" "${extcmdargs[@]}") <("${extcmd[@]}" "$right" "${extcmdargs[@]}")
}
dsorted() {
diffcmd "$@" -u -- sort
}
duniqsorted() {
diffcmd "$@" -u -- sort -u
}
dj() {
diffcmd "$@" -u -- jq .
}
find0() {
find "$@" -print0
}
f() {
dir=$1; shift
name=$1; shift
find "$dir" -name "*${name}*" "$@"
}
f0() {
f "$@" -print0
}
# This is wild! A space in the end makes xargs work with aliases in simple cases, e.g "xargs lh"
# See https://stackoverflow.com/a/59843665/1635525 and "help alias"
alias xargs='xargs '
alias xargs0='xargs -0 '
alias xargsn='xargs -d "\n"'
export EDITOR=vim
export VISUAL=vim
unalias lh lth lth20 2>/dev/null
lh() {
ls -lh "$@" --color=always | l
}
lth() {
ls -lth "$@" --color=always | l
}
lth20() {
ls -lth "$@" --color=always | head -20
}
alias dfh='df -h'
alias bc='bc -l'
wcu() {
sort -u "$@" | wc -l
}
wcl() {
wc -l "$@"
}
sucs() {
sort "$@" | uniq -c | sort -n
}
pstree() {
$(which pstree) $@ | l
}
addpath() {
if [ ! -d "$1" ] ; then
echo "directory '$1' doesn't exist'"
return 1
fi
newp=$(readlink -f "$1")
if ! echo $PATH | grep -E -q "(^|:)$newp($|:)" ; then
export PATH="$newp:$PATH"
else
echo "directory '$newp' is already present in PATH"
fi
return 0
}
rmpath() {
p2del=$(readlink -f "$1")
export PATH="$(echo $PATH | sed -e "s;\(^\|:\)$p2del\(:\|\$\);\1\2;g" -e 's;^:\|:$;;g' -e 's;::;:;g')"
}
mkscreen() {
if [ ! -z "$1" ] ; then
mkdir -p "$HOME/screen/$1" && cd "$HOME/screen/$1" && screen -S "$1"
else
screen -S "$(basename "$PWD")"
fi
}
complete -W "$( [ -d $HOME/screen ] && ls $HOME/screen)" mkscreen
alias slist='screen -list'
pushsshsock() {
[ -S "$SSH_AUTH_SOCK" ] && ln -sf "$SSH_AUTH_SOCK" "$HOME/.ssh/shared_auth_sock"
}
pullsshsock() {
export SSH_AUTH_SOCK="$HOME/.ssh/shared_auth_sock"
}
ssh-persist() {
host="$1" ; shift
if ! ssh -q -O check "$host" 2>/dev/null ; then
( set -x ; ssh -MNf "$host" )
fi
}
svndiff() {
svn diff --diff-cmd=colordiff "$@" | l
}
svninf() {
svn up --parents --set-depth=infinity "$@"
}
pastecol() {
paste -d '|' "$@" | column -t -s '|'
}
columnt() {
column -t -s "$(echo -e '\t')" "$@"
}
xselcol() {
(xsel -b; echo) | columnt | tee /dev/stderr | xsel -b
}
# https://stackoverflow.com/a/20401674/1635525
faketty() {
script --quiet --flush --return --command "$(printf "%q " "$@")" /dev/null
}
# sometimes loses random chars in the middle??
# better to use xsel when available
paste2file() {
if [ -z "$1" ] ; then
echo "Empty filename"
return 1
fi
echo "Use Ctrl+C after a newline to end the input"
stty cbreak
cat > "$1"
stty -cbreak
}
alias urlencode='perl -pe '\''s/([^A-Za-z0-9\n])/sprintf("%%%02X", ord($1))/seg'\'
alias urldecode='perl -pe '\''s/\+/ /g; s/%([[:xdigit:]]{2})/chr(hex($1))/ge'\'
alias cgidecode='perl -pe '\''s/[?&]/\n/g'\'' | urldecode'
alias pullrc=". $HOME/.bashrc"
alias dug='( shopt -s dotglob ; du -csh -- * | grep -P "G\t" )'
alias dus='du -sh * | sort -h -r | l'
withlog() {
if [[ ! -z "$2" && "$2" != -* ]] ; then
cmd_name="${1}_${2}" # e.g. python script name or terraform command name
else
cmd_name="${1}"
fi
cmd_name=${cmd_name//[^a-zA-Z0-9_-.]/_}
mkdir -p "$HOME/logs"
logfile="$HOME/logs/${cmd_name}_$(date +'%Y%m%dT%H%M%S%z').log"
echo -n "CMDLINE: " | tee -a "$logfile"
printf "%q " "$@" | tee -a "$logfile"
echo -ne "\nPWD: $PWD" | tee -a "$logfile"
echo -ne "\nLOGFILE: $logfile" | tee -a "$logfile"
echo -ne "\nSTARTED: $(date --iso-8601=seconds)\n\n" | tee -a "$logfile"
"$@" |& tee -a "$logfile"
echo -e "\n\nFINISHED: $(date --iso-8601=seconds)" | tee -a "$logfile"
echo "LOGFILE: $logfile" | tee -a "$logfile"
}
readlog() {
l "$HOME/logs/$(ls -t "$HOME/logs" | head -1)"
}
b64cert2pem()
{
echo '-----BEGIN CERTIFICATE-----'
cat
echo '-----END CERTIFICATE-----'
}
b64certdecode() {
b64cert2pem | openssl x509 -text -noout | l
}
# python stuff
# to avoid misuse in debian/ubuntu
python() {
echo "Use python3"
false
}
p3() {
withlog python3 -u "$@"
}
# https://stackoverflow.com/a/41386937/1635525
pyclean () {
find . -type f -name '*.py[co]' -delete -o -type d -name __pycache__ -delete
}
epp() {
newp="$PWD"
if [ ! -z "$1" ] ; then
if [ ! -d "$1" ] ; then
echo "directory '$1' doesn't exist'"
return 1
fi
newp="$1"
fi
export PYTHONPATH=$(readlink -f "$newp")
}
vhich() {
echo VIRTUAL_ENV=$VIRTUAL_ENV
}
vactivate() {
if [ ! -z "$1" ] ; then
if [ ! -d "$1" ] ; then
echo "directory '$1' doesn't exist'"
return 1;
fi
export VIRTUAL_ENV="$1"
fi
if [ -n "$VIRTUAL_ENV" -a -f "$VIRTUAL_ENV/bin/activate" ] ; then
source "$VIRTUAL_ENV/bin/activate"
elif [ -f "$PWD/venv/bin/activate" ] ; then
source "$PWD/venv/bin/activate"
else
echo "Can't find virtual environment"
return 1
fi
}
mkvenv() {
PYTHON=${1:-python3}
$PYTHON -m venv --prompt $(basename $PWD) ./venv \
&& vactivate ./venv \
&& vhich \
&& pip3 install --upgrade pip \
&& python3 --version
}
pydict2json() {
python3 -c 'import json; print(json.dumps(eval(input())))' | j . "$@"
}
# docker stuff
dps() {
docker ps "$@" | l
}
dim() {
docker image ls | l
}
dlast() {
docker image ls -q | head -1
}
drunning() {
docker ps -q | head -1
}
dshell() {
container=${1:-$(drunning)}
docker exec -it "$container" env TERM=xterm-256color /bin/bash
}
drun() {
image=${1:-$(dlast)}
docker run -d --init "$image" sleep inf && dshell
}
drundev() {
image=${1:-$(dlast)}
docker run -d --init --network=host --mount "type=bind,source=$HOME,target=/host_home" "$image" sleep inf && dshell
}
dstop() {
container=${1:-$(drunning)}
docker stop "$container"
}
dsetup() {
container=${1:-$(drunning)}
docker exec -it "$container" bash -c "apt-get update && apt-get -y install vim less iproute2 strace psmisc"
}
# https://stackoverflow.com/questions/21200304/docker-how-to-show-the-diffs-between-2-images
ddiff() {
cmd="$1"; shift
image1="$1"; shift
image2="$1"; shift
dir="${1:-/}"
out1="/tmp/dockerfiles.$image1.txt"
out2="/tmp/dockerfiles.$image2.txt"
docker run "$image1" bash -c "find $dir -xdev -type f | sort | $cmd" > "$out1"
docker run "$image2" bash -c "find $dir -xdev -type f | sort | $cmd" > "$out2"
d "$out1" "$out2"
echo d "$out1" "$out2"
}
dlistdiff() {
ddiff "cat" "$@"
}
dhashdiff() {
ddiff "xargs -n 1 --delimiter '\n' md5sum" "$@"
}
# AWS stuff
awsprofile() {
if [ -z "$1" ] ; then
echo "AWS_PROFILE=$AWS_PROFILE"
else
export AWS_PROFILE="$1"
fi
if ! aws sts get-caller-identity >/dev/null 2>&1 ; then
aws sso login
fi
echo AWS_PROFILE="$AWS_PROFILE"
aws sts get-caller-identity
}
file_or_null() {
if [ -f "$1" ] ; then
echo "$1"
else
echo "/dev/null"
fi
}
AWS_PROFILE_COMPLETE=$(grep -h '\[' $(file_or_null ~/.aws/config) $(file_or_null ~/.aws/credentials) | sed -e 's/.* //;' | tr -d '[]' | sort -u)
complete -W "$AWS_PROFILE_COMPLETE" awsprofile
unalias awsssm 2>/dev/null
awsssm() {
rlwrap -I aws ssm start-session --target "$@"
}
# all branch-specific changes should be below this line