-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path.bashrc_linux
More file actions
645 lines (549 loc) · 19.4 KB
/
Copy path.bashrc_linux
File metadata and controls
645 lines (549 loc) · 19.4 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
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
# ~/.bashrc_linux
# ============================================================================
# Bash shell configuration - Linux
# Copyright (c) Corey Goldberg (https://github.com/cgoldberg)
# ============================================================================
#
# bbbbbbbb
# b::::::b hhhhhhh
# b::::::b h:::::h
# b::::::b h:::::h
# b:::::b h:::::h
# b:::::bbbbbbbbb aaaaaaaaaaaaa ssssssssss h::::h hhhhh
# b::::::::::::::bb a::::::::::::a ss::::::::::s h::::hh:::::hhh
# b::::::::::::::::b aaaaaaaaa:::::ass:::::::::::::s h::::::::::::::hh
# b:::::bbbbb:::::::b a::::as::::::ssss:::::sh:::::::hhh::::::h
# b:::::b b::::::b aaaaaaa:::::a s:::::s ssssss h::::::h h::::::h
# b:::::b b:::::b aa::::::::::::a s::::::s h:::::h h:::::h
# b:::::b b:::::b a::::aaaa::::::a s::::::s h:::::h h:::::h
# b:::::b b:::::ba::::a a:::::assssss s:::::s h:::::h h:::::h
# b:::::bbbbbb::::::ba::::a a:::::as:::::ssss::::::sh:::::h h:::::h
# b::::::::::::::::b a:::::aaaa::::::as::::::::::::::s h:::::h h:::::h
# b:::::::::::::::b a::::::::::aa:::as:::::::::::ss h:::::h h:::::h
# bbbbbbbbbbbbbbbb aaaaaaaaaa aaaa sssssssssss hhhhhhh hhhhhhh
#
# ============================================================================
# only use this file on Linux
if [[ "${OSTYPE}" != "linux"* ]]; then
return
fi
# enable bash completion
source /usr/share/bash-completion/bash_completion
# enable git prompt
source /usr/lib/git-core/git-sh-prompt
# set a colored prompt with:
# - user:dir
# - git branch with state (if in a git repo)
export GIT_PS1_HIDE_IF_PWD_IGNORED=1
export GIT_PS1_SHOWDIRTYSTATE=1
export GIT_PS1_SHOWSTASHSTATE=1
export GIT_PS1_SHOWUPSTREAM="auto"
PS1=\
'\[\033[1;38;5;214m\]\u\[\033[0m\]'\
'\[\033[1;38;5;255m\]:\[\033[00m\]'\
'\[\033[1;38;5;123m\]\w\[\033[00m\]'\
'\[\033[0;38;5;046m\]$(__git_ps1 " (%s)")\[\033[0m\] '\
'\[\033[1;38;5;255m\]\$ \[\033[0m\]'
# make less more friendly for non-text input files
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"
# build and manage python versions with pyenv
PY_VERSIONS=("3.14" "3.13" "3.12" "3.11" "3.10" "pypy3.11")
if [ -d "${HOME}/.pyenv" ]; then
export PYENV_ROOT="${HOME}/.pyenv"
[[ -d "${PYENV_ROOT}/bin" ]] && export PATH="${PYENV_ROOT}/bin:${PATH}"
eval "$(pyenv init - bash)"
eval "pyenv global ${PY_VERSIONS[*]}"
fi
# ======================================================
#
# 888 d8b
# 888 Y8P
# 888
# 8888b. 888 888 8888b. .d8888b .d88b. .d8888b
# "88b 888 888 "88b 88K d8P Y8b 88K
# d888888 888 888 .d888888 "Y8888b. 88888888 "Y8888b.
# 88 888 888 888 888 888 X88 Y8b. X88
# Y888888 888 888 "Y888888 88888P' "Y8888 88888P'
#
# ======================================================
# expand aliases when running sudo
alias sudo="sudo "
# expand aliases when running watch
alias watch="watch "
# exit shell
alias x="exit"
# look all busy and fancy to non-technical people
alias busy="cat /dev/urandom | hexdump -C | \grep --color=always 'ca fe'"
# disk space
alias df="df --human-readable --print-type --sync"
# watch disk space
alias diskwatch="watch --no-wrap --interval 5 --exec bash -c diskspace"
alias dw="diskwatch"
# monitor cpu and device/partition i/o stats
alias iostat="iostat -p --human --pretty 2"
# monitor bandwidth by process/connection
alias bw="bandwhich --unit-family=si-bits"
# monitor bandwidth by interface
alias bmon="bmon --use-bit --use-si --policy='wlan*,eno*,eth*'"
# monitor resources (cpu/mem/disk/net/procs)
# see ~/.config/btop/btop.conf
alias btop="btop --preset 0"
# kill process group by name (case-insensitive)
alias killall="killall --ignore-case --process-group --wait"
# don't leave .wget-hsts file in home directory
alias wget="wget --hsts-file=/dev/null"
# show tcp/udp listening sockets and summary of network connections
alias listening="ss --summary --listening --numeric --processes --resolve --tcp --udp"
# show tcp/udp non-listening sockets and summary of network connections
alias sockets="ss --summary --numeric --processes --resolve --tcp --udp"
# display slideshow of images in given directory
alias slideshow="mpv --fullscreen --shuffle --cursor-autohide=0 \
--directory-mode=recursive --image-display-duration=4 --keep-open=no \
--loop-playlist=inf --osd-level=0"
# =========================================================================================
#
# .o88o. . o8o
# 888 `" .o8 `"'
# o888oo oooo oooo ooo. .oo. .ooooo. .o888oo oooo .ooooo. ooo. .oo. .oooo.o
# 888 `888 `888 `888P"Y88b d88' `"Y8 888 `888 d88' `88b `888P"Y88b d88( "8
# 888 888 888 888 888 888 888 888 888 888 888 888 `"Y88b.
# 888 888 888 888 888 888 .o8 888 . 888 888 888 888 888 o. )88b
# o888o `V88V"V8P' o888o o888o `Y8bod8P' "888" o888o `Y8bod8P' o888o o888o 8""888P'
#
# =========================================================================================
# launch git client gui
# opens repo in current directory or as specified in arg
gitte() {
systemd-run --user --quiet \
flatpak run de.wwwtech.gitte "${1:-${PWD}}"
ok "Gitte launched"
}
# launch markdown viewer gui
# opens README.MD or file specified in arg
apostrophe() {
systemd-run --user --quiet \
flatpak run org.gnome.gitlab.somas.Apostrophe "$(realpath "${1:-README.md}")"
ok "Apostrophe launched"
}
# show disk space used by root filesystem and mounted filesystems
diskspace() {
\df --human-readable --sync --type=ext4 --type=btrfs --type=cifs --type=exfat |
awk 'NR==1 || $NF == "/" || $NF ~ /^\/(mnt|media)\//' |
sed '1{p;s/./-/g;}'
}
# make it available in non-interactive shells so watch can see it
export -f diskspace
alias ds="diskspace"
# show internal ip addresses
ips() {
ipv4=$(ip -4 route get 1.1.1.1 2>/dev/null)
ipv4=${ipv4#*src }
ipv4=${ipv4%% *}
ipv6=$(ip -6 route get 2606:4700:4700::1111 2>/dev/null)
ipv6=${ipv6#*src }
ipv6=${ipv6%% *}
if [ -n "${ipv4}" ]; then
echo "IPv4: ${ipv4}"
fi
if [ -n "${ipv6}" ]; then
echo "IPv6: ${ipv6}"
fi
}
# open url or file in default browser
web() {
if [ -z "$1" ]; then
err "please enter a file or url"
return 1
fi
sensible-browser "$1"
}
# show blocked firewall connections from systemd logs in reverse order
firewall-blocks() {
local blue="\x1b[34m"
local green="\x1b[32m"
local reset="\x1b[0m"
sudo journalctl -k -r -g 'UFW' | \
awk -v blue="${blue}" -v green="${green}" -v reset="${reset}" '{
printf "%s %s %s [UFW BLOCK]", $1,$2,$3
for(i=1;i<=NF;i++){
if($i ~ /^SRC=/){
split($i,a,"=")
printf " SRC=%s%s%s", blue, a[2], reset
}
if($i ~ /^DST=/){
split($i,a,"=")
printf " DST=%s%s%s", green, a[2], reset
}
if($i ~ /^PROTO=/) printf " %s", $i
if($i ~ /^SPT=/) printf " %s", $i
if($i ~ /^DPT=/) printf " %s", $i
}
printf "\n"
}' | less
}
# run a command and send a desktop notification when it finishes
# - exit status from command is returned
# - example: notify sleep 5
notify() {
local cmd="$*"
local start=${EPOCHREALTIME}
"$@"
local status=$?
local elapsed=$(awk "BEGIN { printf \"%.2f\", ${EPOCHREALTIME} - ${start} }")
if [ ${status} -eq 0 ]; then
notify-send "Done (${elapsed}s)" "${cmd}"
else
notify-send -u critical "Failed (${elapsed}s, exit ${status})" "${cmd}"
fi
return ${status}
}
# run jekyll server for blog and open local url in browser
blog() {
local blog_dir="${HOME}/code/cgoldberg.github.io"
local jekyll_server="127.0.0.1:4000"
if [ ! -d "${blog_dir}" ]; then
err "${blog_dir} not found"
return 1
fi
if ! type bundle >/dev/null 2>&1; then
err "bundler not found"
return 1
fi
killblog
cd "${blog_dir}"
bundle install >/dev/null
bundle exec jekyll clean >/dev/null
echo "running server at: ${jekyll_server}"
bundle exec jekyll s >/dev/null & disown # silence stdout
sleep 3.5
echo "opening browser at: http://${jekyll_server}"
web "http://${jekyll_server}"
}
# kill jekyll server running blog
killblog() {
local blog_dir="${HOME}/code/cgoldberg.github.io"
local jekyll_proc="jekyll s"
local jekyll_server="127.0.0.1:4000"
if [ ! -d "${blog_dir}" ]; then
err "${blog_dir} not found. no server running"
return 1
fi
if [[ $(psgrep "${jekyll_proc}") ]]; then
echo "killing server at: ${jekyll_server}"
pkill -9 -f "${jekyll_proc}" && pidwait -f "${jekyll_proc}"
psgrep "${jekyll_proc}"
fi
}
# trim audio file length (mp3 or flac)
# - re-encodes flac with default compression
# - trims mp3 without re-encoding
# - usage: trim-audio <filename> <start-hms> <end-hms>
# - example: trim-audio input.flac 00:00:00 00:08:35
trim-audio() {
local time_re="^([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$"
if [ "$#" -ne 3 ]; then
err "expected 3 args"
return 1
fi
if [[ ! "$2" =~ $time_re || ! "$3" =~ $time_re ]]; then
err "args 2 and 3 must be valid times (hh:mm:ss)"
return 1
fi
local out
if [[ "$1" == *.flac ]]; then
out="${1%.*}_trim.flac"
ffmpeg -i "$1" -ss "$2" -to "$3" -c:a flac "$out"
elif [[ "$1" == *.mp3 ]]; then
out="${1%.*}_trim.mp3"
ffmpeg -i "$1" -ss "$2" -to "$3" -c copy "$out"
else
err "unknown file type"
return 1
fi
}
# whack the thumbnail cache
clean-thumbnails() {
rm -rf "${HOME}/.cache/thumbnails/"
ok "cleaned thumbnail cache"
}
# whack the package resource lists
clean-apt-index() {
sudo rm -rf /var/lib/apt/lists/*
ok "cleaned package resource lists"
}
# whack all system logs
clean-system-logs() {
local no_prompt="false"
case "$1" in
-y|--yes)
no_prompt="true"
;;
esac
echo "stopping systemd-journald service..."
sudo systemctl --quiet stop systemd-journald
echo "removing systemd-journald logs..."
sudo rm -rf /var/log/journal/*
sudo rm -rf /run/log/journal/*
echo "truncating all system log files..."
sudo find /var/log -type f -name "*.log" -exec truncate -s 0 {} \;
sudo truncate -s 0 /var/log/wtmp /var/log/wtmp.db
echo "removing rotated log files and their archives..."
sudo find /var/log -type f -name "*.log.*" -exec rm -f {} \;
echo "removing other archived log files..."
sudo find /var/log -type f \( -name "*.gz" -o -name "*.xz" -o -name "*.tar" \) -exec rm -f {} \;
echo "restarting systemd-journald service..."
sudo systemctl start systemd-journald
echo
ok "cleaned all system logs"
if [[ "${no_prompt}" == "false" ]]; then
echo
echo "reboot now?"
echo
local result
select result in "yes" "no"; do
if [ "${result}" == "yes" ]; then
echo
ok "rebooting now..."
sudo reboot
fi
break
done
fi
}
# deb package maintenance
apt-up() {
run() {
"$@" || { err "command failed: $*"; return 1; }
}
write() {
printf "\n${BOLD}%s${RESET}\n" "$*" >&2
}
write "updating list of available packages..."
run sudo apt update --no-allow-insecure-repositories || return 1
write "fixing broken dependencies..."
run sudo apt install --fix-broken || return 1
write "upgrading installed packages..."
run sudo apt -o APT::Get::Always-Include-Phased-Updates=true full-upgrade || return 1
write "removing unused packages..."
run sudo apt autoremove --purge || return 1
write "removing cached packages..."
run sudo apt clean || return 1
write "purging orphaned package configs..."
local rc_pkgs=$(dpkg-query -W -f='${db:Status-Abbrev} ${binary:Package}\n' |
grep '^rc' | awk '{print $2}')
if [ -n "${rc_pkgs}" ]; then
echo "$(wc -w <<<"${rc_pkgs}") orphaned package configs found"
sudo dpkg --purge ${rc_pkgs} || return 1
fi
local num_pkgs=$(dpkg-query -W -f='${db:Status-Abbrev}\n' | grep -c '^ii')
echo
ok "done (${num_pkgs} packages installed)"
unset -f run
unset -f write
}
# flatpak maintenance
flat-up() {
run() {
"$@" || { err "command failed: $*"; return 1; }
}
write() {
printf "\n${BOLD}%s${RESET}\n" "$*" >&2
}
write "upgrading apps and runtimes..."
flatpak update || return 1
write "removing unused runtimes..."
flatpak uninstall --unused || return 1
local app_count=$(flatpak list --app | wc -l)
local runtime_count=$(flatpak list --runtime | wc -l)
echo
ok "done (${app_count} apps and ${runtime_count} runtimes installed)"
unset -f run
unset -f write
}
# show which system package an executable on PATH comes from
apt-which-pkg() {
if [ -z "$1" ]; then
err "no package specified"
return 1
fi
bin_path="$(realpath $(\which $1) 2>/dev/null)"
if [ -z "${bin_path}" ]; then
err "'$1' not found on PATH"
return 1
else
pkg="$(dpkg -S ${bin_path} 2>/dev/null)"
if [ -z "${pkg}" ]; then
err "'$1' not found in a package"
return 1
fi
fi
ok "'${bin_path}' found in package '${pkg%%:*}'"
}
# check all installed packages against backports and show if a newer version is available
# - skips packages named lib*
apt-check-available-backports() {
local debian_codename installed_version backports_version pkg
sudo apt-get update --no-allow-insecure-repositories >/dev/null
debian_codename=$(lsb_release -cs)
echo -e "\nchecking installed packages for newer versions in ${debian_codename}-backports..."
echo
printf "%b%-40s%b %-40s %-40s\n" \
"${BOLD}${GREEN}" "Package" "${RESET}" \
"Installed Version" "Backports Version"
printf '%.0s-' {1..120}; echo
for pkg in $(dpkg --get-selections | grep -v deinstall | cut -f1); do
if [[ "${pkg}" != lib* ]]; then
installed_version=$(dpkg-query -W -f='${Version}' "${pkg}")
backports_version=$(apt-cache madison "${pkg}" | grep -oP \
".*${debian_codename}-backports.*" | awk '{print $3}' | head -n 1)
# if backports_version is empty or invalid, skip this package
if [[ -z "${backports_version}" || ! "${backports_version}" =~ ^[0-9] ]]; then
continue
fi
if dpkg --compare-versions "${backports_version}" gt "${installed_version}"; then
printf "%b%-40s%b %-40s %-40s\n" \
"${BOLD}${GREEN}" "${pkg%%:*}" "${RESET}" \
"${installed_version}" "${backports_version}"
fi
fi
done
}
# install or upgrade a package from backports
apt-install-package-from-backports() {
if [ -z "$1" ]; then
err "no package specified"
return 1
fi
local debian_codename=$(lsb_release -cs)
sudo apt update --no-allow-insecure-repositories
sudo apt install -t "${debian_codename}-backports" "$1"
}
# show locally installed .deb packages
apt-show-local-pkgs() {
apt list --installed 2>/dev/null | \grep installed,local | cut -d'/' -f1
}
# update locally installed .deb packages
update-apt-local-pkgs() {
if [ "$(arch)" == "aarch64" ]; then
local architecture="arm64"
else
local architecture="amd64"
fi
local packages=$(apt-show-local-pkgs)
local original_dir=${PWD}
rm -f /tmp/*.deb
for pkg in ${packages[@]}; do
local pkg_repo_dir="${HOME}/code/${pkg}"
if [ ! -d "${pkg_repo_dir}" ]; then
echo
err "can't find git repo for package: ${pkg}"
echo
echo "locally cloned repos are required at:"
for pkg in ${packages[@]}; do
echo " - ${HOME}/code/${pkg}"
done
return 1
fi
done
for pkg in ${packages[@]}; do
local pkg_repo_dir="${HOME}/code/${pkg}"
cd "${pkg_repo_dir}"
echo
echo "downloading latest release of package: '${pkg}'..."
gh release download --clobber --pattern="*${architecture}.deb" --dir="/tmp"
for deb in /tmp/*"${architecture}.deb"; do
if [[ ${deb} != *musl* ]]; then
echo "installing..."
sudo apt install -y "${deb}"
echo
fi
rm "${deb}"
cd "${original_dir}"
done
done
cd "${original_dir}"
ok "done upgrading locally installed packages"
}
# clean cargo cache and registry
clean-rust() {
cargo cache --autoclean
if [ -d "${HOME}/.cargo" ]; then
rm -rf "${HOME}/.cargo/registry"
rm -rf "${HOME}/.cargo/git"
fi
}
# update all rust toolchains and binaries that were installed with cargo
update-rust-bins() {
rustup update
echo
cargo install-update --all
echo
clean-rust
ok "updated all rust toolchains and binaries"
}
# update python interpreters with pyenv
# - update pyenv
# - uninstall every python installation
# - install latest version of each python interpreter
# - update pip in each python installation
# - re-install pipx with most recent python interpreter and update all pipx applications
update-pyenv() {
if [ ! -d "${HOME}/.pyenv" ]; then
err "pyenv not configured"
return 1
fi
if [ -n "${VIRTUAL_ENV}" ]; then
echo "deactivating venv"
deactivate
fi
echo
pyenv global system
echo "updating pyenv..."
pyenv update
pyenv rehash
echo
local minor_versions=( $(pyenv versions --bare | \grep "${py_version}") )
for minor_version in "${minor_versions[@]}"; do
echo "uninstalling Python ${minor_version}..."
pyenv uninstall --force "${minor_version}"
echo
done
for py_version in "${PY_VERSIONS[@]}"; do
echo "installing latest version of Python ${py_version}..."
# build with optimizations for maximum performance (builds take longer)
env PYTHON_CONFIGURE_OPTS='--enable-optimizations --with-lto' \
PYTHON_CFLAGS='-march=native -mtune=native' pyenv install \
--force --verbose "${py_version}"
pyenv global "${py_version}"
echo "installed $(python3 --version)"
echo
echo "updating pip..."
PIP_REQUIRE_VIRTUALENV=false python3 -m pip install --upgrade \
--upgrade-strategy=eager pip
echo
done
pyenv rehash
pyenv global "${PY_VERSIONS[0]}"
pip-uninstall-all
gpip-install pipx
pipx-upgrade-all
echo
pyenv global ${PY_VERSIONS[*]}
echo "global python versions installed:"
pyenv global
echo
ok "done updating pythons"
}
# activate python virtual env
activate() {
if [ -d venv/ ]; then
source ./venv/bin/activate
fi
}
# create/activate python virtual env
venv() {
if [ ! -d venv/ ]; then
python3 -m venv --upgrade-deps venv
fi
activate
}