.bash_aliases 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. # Basic .bash_alias file for command line shortcuts
  2. # Save this as ~/.bash_aliases
  3. # On your ~/.bashrc add `. ~/.bash_aliases`
  4. # Reboot or relogin
  5. # To try without reboot (on current bash session), run `touch ~/.bash_aliases`
  6. su_bin=doas
  7. [ -n "$(command -v sudo)" ] && su_bin=sudo
  8. ## ---- Package Manager based... ---- ##
  9. if [ -x "$(command -v apt)" ]; then
  10. # Debian based
  11. alias update-system="$su_bin apt update && $su_bin apt upgrade -y ; sync"
  12. alias install-package="$su_bin apt install -y "
  13. alias remove="$su_bin apt autoremove -y "
  14. alias search='apt search '
  15. alias clean-packages="$su_bin apt clean"
  16. elif [ -x "$(command -v pacman)" ]; then
  17. # Arch linux based
  18. alias update-system="$su_bin pacman -Syu --noconfirm ; sync"
  19. alias update-mirrors="$su_bin "'reflector --verbose --latest 5 \
  20. --sort rate --save /etc/pacman.d/mirrorlist'
  21. alias install-package="$su_bin pacman --noconfirm -S "
  22. alias remove="$su_bin pacman --noconfirm -Rs "
  23. alias remove-orphans="pacman -Qtdq | $su_bin pacman -Rns -"
  24. alias search='pacman -Ss '
  25. alias clean-packages="$su_bin pacman -Scc"
  26. alias find-pacs='find /etc -regextype posix-extended \
  27. -regex ".+\.pac(new|save)" 2> /dev/null'
  28. elif [ -x "$(command -v xbps-install)" ]; then
  29. # Void Linux based
  30. alias update-system="$su_bin xbps-install -Syu ; sync"
  31. alias install-package="$su_bin xbps-install -y "
  32. alias remove="$su_bin xbps-remove -Ry "
  33. alias search='xbps-query -Rs '
  34. alias clean-packages="$su_bin xbps-remove -O"
  35. elif [ -x "$(command -v zypper)" ]; then
  36. # Suse based
  37. alias update-system="$su_bin zypper refresh && $su_bin zypper refresh ; sync"
  38. alias install-package="$su_bin zypper install -y "
  39. alias remove="$su_bin zypper remove -y "
  40. alias search='zypper search '
  41. alias clean-packages="$su_bin zypper clean metadata && $su_bin zypper clean packages"
  42. elif [ -x "$(command -v rpm)" ]; then
  43. # RPM based
  44. alias update-system="$su_bin yum -y update ; sync"
  45. alias install-package="$su_bin yum install -y "
  46. alias remove="$su_bin yum remove -y "
  47. alias search='yum search '
  48. alias clean-packages="$su_bin yum clean metadata && $su_bin yum clean packages"
  49. elif [ -x "$(command -v guix)" ]; then
  50. # Guix/GuixSD based
  51. alias update-system='guix pull && guix package -u ; sync'
  52. alias install-package='guix install '
  53. alias remove='guix remove '
  54. alias search='guix search '
  55. elif [ -x "$(command -v pkg)" ]; then
  56. # FreeBSD systems
  57. alias update-system="$su_bin pkg update && $su_bin pkg upgrade -y ; sync"
  58. alias install-package="$su_bin pkg install -y "
  59. alias remove="$su_bin pkg remove -y "
  60. alias remove-orphans="$su_bin pkg autoremove -y"
  61. alias search='pkg search '
  62. alias clean-packages="$su_bin pkg clean -y"
  63. elif [ -x "$(command -v pkg_add)" ]; then
  64. # OpenBSD systems
  65. alias update-system="$su_bin pkg_add -u ; sync"
  66. alias install-package="$su_bin pkg_add "
  67. alias remove="$su_bin pkg_delete "
  68. alias remove-orphans='$su_bin pkg_delete -a'
  69. alias search='pkg_info -c -Q '
  70. elif [ -x "$(command -v apk)" ]; then
  71. # Alpine Linux
  72. alias update-system="$su_bin apk update && $su_bin apk upgrade ; sync"
  73. alias install-package="$su_bin apk add "
  74. alias remove="$su_bin apk del "
  75. alias search='apk search '
  76. alias clean-packages="$su_bin apk cache clean"
  77. fi
  78. ## ---- Service Related ---- ##
  79. # usage:
  80. # handleService start|stop|restart|enable|disable <service name>
  81. # handleService list
  82. handleService() {
  83. if [ -x "$(command -v systemctl)" ]; then # for SystemD
  84. service_list=$(systemctl list-unit-files -l)
  85. if [ ${1} == 'list' ]; then
  86. echo "$service_list"
  87. else
  88. if [[ `echo "$service_list" | grep "^${2}\.service"` != '' ]]; then
  89. $su_bin systemctl ${1} ${2}
  90. else
  91. echo "$2 service not found"
  92. fi
  93. fi
  94. # The "-f" check should be first for FreeBSD
  95. elif [ -f "/usr/sbin/service" ] || [[ "$(cat /proc/1/comm 2>/dev/null)" = "init" ]]; then # for SysVinit or "service" binary
  96. # to check if any of the commands fail and return a non-zero value for nonexisting service
  97. if [ "$(uname -s)" = "FreeBSD" ]; then
  98. service_list=$(service -l)
  99. echo "$service_list" | grep ${2}
  100. else
  101. service_list=$($su_bin service --status-all)
  102. $su_bin service ${2} status
  103. fi
  104. retcode="$?"
  105. if [ ${1} == 'list' ]; then
  106. echo "$service_list"
  107. else
  108. if [ "$retcode" = "0" ]; then
  109. $su_bin service ${2} ${1}
  110. else
  111. echo "$2 service not found"
  112. fi
  113. fi
  114. elif [ -x "$(command -v rcctl)" ]; then # for OpenBSD
  115. service_list=$(rcctl ls all)
  116. if [ ${1} == 'list' ]; then
  117. echo "$service_list"
  118. else
  119. if [[ `echo "$service_list" | grep "^${2}$"` != '' ]]; then
  120. $su_bin rcctl ${1} ${2}
  121. else
  122. echo "$2 service not found"
  123. fi
  124. fi
  125. elif [[ "$(cat /proc/1/comm 2>/dev/null)" = "runit" ]]; then # for runit
  126. enabled_dir='/etc/runit/runsvdir/current/'
  127. if [ -d '/etc/runit/sv/' ]; then # Artix
  128. service_dir='/etc/runit/sv/'
  129. else # Void Linux and possibly others
  130. service_dir='/etc/sv/'
  131. fi
  132. service_list=$(ls -1 ${service_dir})
  133. if [ ${1} == 'list' ]; then
  134. echo "${service_list}"
  135. else
  136. if [ `echo "${service_list}" | grep "^${2}$"` ]; then
  137. if [ ${1} == 'enable' ]; then
  138. $su_bin ln -s ${service_dir}${2} ${enabled_dir}${2}
  139. elif [ ${1} == 'disable' ]; then
  140. $su_bin rm ${enabled_dir}${2}
  141. else
  142. if [ -d ${enabled_dir}${2} ]; then
  143. $su_bin sv ${1} ${2}
  144. fi
  145. fi
  146. else
  147. echo "$2 service not found"
  148. fi
  149. fi
  150. fi
  151. }
  152. alias start-service='handleService start'
  153. alias stop-service='handleService stop'
  154. alias restart-service='handleService restart'
  155. alias enable-service='handleService enable'
  156. alias disable-service='handleService disable'
  157. alias list-services='handleService list'
  158. handleServer() {
  159. handleService ${1} apache
  160. handleService ${1} apache2
  161. handleService ${1} httpd
  162. handleService ${1} mariadb
  163. handleService ${1} mysqld
  164. }
  165. alias start-server='handleServer start'
  166. alias stop-server='handleServer stop'
  167. alias restart-server='handleServer restart'
  168. # killall alternative for systems without it
  169. if [ ! -x "$(command -v killall)" ]; then
  170. killall () {
  171. echo `pidof $1` && $su_bin kill -9 `pidof $1` || echo "$1 not running"
  172. }
  173. fi
  174. ## ---- Git Related ---- ##
  175. # Returns 0 if pwd is git root dir
  176. check_git_root() {
  177. [ -d '.git' ] && return 0 || return 1
  178. }
  179. function _gtac {
  180. if check_git_root; then
  181. git add .
  182. git commit -m "$1"
  183. else
  184. echo 'Error: not git repo root'
  185. return 1
  186. fi
  187. }
  188. alias gtpull='git pull --ff-only'
  189. alias gtpush='git push'
  190. alias gts='git status'
  191. alias gtd='git diff'
  192. alias gtac='_gtac'
  193. alias gtrevert='check_git_root && ( git reset && git checkout . && git clean -fdx ) || echo "Error: not git repo root"'
  194. alias gtcl='git config --list'
  195. alias gtc='git config'
  196. alias gtl='git log'
  197. ## ---- Misc ---- ##
  198. alias list='ls -hN --color=auto --group-directories-first'
  199. alias update-grub="$su_bin grub-mkconfig -o /boot/grub/grub.cfg"
  200. alias download-youtube='youtube-dl --write-auto-sub --embed-subs \
  201. -f "(mp4)[height<480]" '
  202. alias list-groups='cut -d: -f1 /etc/group | sort'
  203. if [ -x "$(command -v nvim)" ]; then
  204. alias v='nvim'
  205. elif [ -x "$(command -v vim)" ]; then
  206. alias v='vim'
  207. elif [ -x "$(command -v vi)" ]; then
  208. alias v='vi'
  209. fi
  210. alias show-metadata='exiftool '
  211. alias strip-metadata='exiftool -all= -overwrite_original'
  212. alias run-qemu="qemu-system-$(uname -m) -m 1024 -net nic -net user \
  213. -soundhw all -machine accel=kvm -cdrom "
  214. alias update-vpns="$su_bin python3 ~/.scripts/autovpngate.py"
  215. alias ip-info='curl -s https://ifconfig.co/json || wget -qO- https://ifconfig.co/json || echo "failed!"'
  216. alias cpu-usage="awk '{u=\$2+\$4; t=\$2+\$4+\$5; if (NR==1){u1=u; t1=t;} \
  217. else print (\$2+\$4-u1) * 100 / (t-t1) \"%\"; }' \
  218. <(grep 'cpu ' /proc/stat) <(sleep 1;grep 'cpu ' /proc/stat)"
  219. internal_battery_usage() { for bn in /sys/class/power_supply/BAT*; do [ -d "${bn}" ] && echo "Battery ${bn:27:1}: $(cat ${bn}/capacity)% ($(cat ${bn}/status))"; done }
  220. alias battery-usage='internal_battery_usage'
  221. # Budgie related
  222. alias restart-budgie='nohup budgie-panel --replace&'
  223. # GNOME related
  224. alias launch-dark='setsid env GTK_THEME=Adwaita:dark '
  225. alias start-gnome-wayland='QT_QPA_PLATFORM=wayland XDG_SESSION_TYPE=wayland dbus-run-session gnome-session'
  226. alias start-gnome-xorg='GDK_BACKEND=x11 gnome-session'