From 9537987165c6d56e30cdedb553780d45fa1b6cd2 Mon Sep 17 00:00:00 2001 From: Pratik Tripathy Date: Mon, 13 Oct 2025 19:56:06 +0530 Subject: [PATCH] feat(shell): Add aliases from dev_prod project, remove aliases required for servers --- linux/.config/shell/aliases_linux.sh | 160 +++++++++++---------------- 1 file changed, 65 insertions(+), 95 deletions(-) diff --git a/linux/.config/shell/aliases_linux.sh b/linux/.config/shell/aliases_linux.sh index 90df5a4..e971787 100644 --- a/linux/.config/shell/aliases_linux.sh +++ b/linux/.config/shell/aliases_linux.sh @@ -3,6 +3,57 @@ # Kitty & Ghostty terminfo aren't available on most servers alias ssh="TERM=xterm-256color ssh" +alias ls='ls --color=auto --hyperlink' +alias ll='ls -alhF' +alias la='ls -Ah' +alias lsa="ls -lAFhZ" +alias printpath="echo $PATH | tr : '\n'" + +# Templates +[ ! -f "$GITIGNORE_TEMPLATE" ] || alias cp_gi='cp ${GITIGNORE_TEMPLATE} .' +[ ! -f "$PRETTIER_TEMPLATE" ] || alias cp_prc='cp ${PRETTIER_TEMPLATE} .' +[ ! -f "$PRETTIER_IGNORE_TEMPLATE" ] || alias cp_prigr='cp ${PRETTIER_IGNORE_TEMPLATE} .' +[ ! -f "$ESLINT_TEMPLATE" ] || alias cp_eslint='cp ${ESLINT_TEMPLATE} .' + +# Coding +command -v tldr >/dev/null && alias tldr="tldr --platform=linux" +command -v tldr >/dev/null && alias h="tldr" +command -v fzf >/dev/null && alias path="printenv | grep ^PATH= | sed 's/^PATH=//' | tr ':' '\n' | fzf" +command -v podman >/dev/null && alias docker=podman + +# Git +alias cd_root='cd $(git rev-parse --show-toplevel 2>/dev/null || echo ".")' +alias cd_git_root=cd_root + +git_push_all_changes() { + if [ -z "$1" ] || [ "$1" = " " ]; then + echo "Please provide a commit message." + return 126 + fi + git add . && git commit -am "${1}" && git push +} + +cp_git_precommit() { + if [ ! -f "$GIT_PRECOMMIT_TEMPLATE" ]; then + echo "Git Pre-commit template does not exist" + return 1 + fi + + hooks_dir="$(pwd)/.git/hooks" + + if [ ! -d "$hooks_dir" ]; then + echo "Git repository not found under $(pwd)" + return 2 + fi + + if [ -f "$hooks_dir/$(basename "$GIT_PRECOMMIT_TEMPLATE")" ]; then + echo "pre-commit hook already exist. Skipping..." + return 0 + fi + + cp "$GIT_PRECOMMIT_TEMPLATE" "$hooks_dir" && printf "Pre-commit hook template copied to %s\n" "$hooks_dir" +} + # Directories and Directory listings dir_size() { if [ "$1" = "" ]; then @@ -14,36 +65,36 @@ dir_size() { du -ah "$dir" --max-depth=1 | sort -hr } -up(){ +up() { update_command="" # Detect package manager and set package manager commands - if command -v dnf > /dev/null 2>&1; then + if command -v dnf >/dev/null 2>&1; then update_command="sudo dnf upgrade --refresh && sudo dnf system-upgrade download --releasever=$(rpm -E %fedora) && sudo dnf autoremove" - elif command -v pkcon > /dev/null 2>&1; then + elif command -v pkcon >/dev/null 2>&1; then update_command="sudo pkcon refresh && sudo pkcon update && sudo apt dist-upgrade && sudo apt autoremove" - elif command -v apt-get > /dev/null 2>&1; then + elif command -v apt-get >/dev/null 2>&1; then update_command="sudo apt-get update && sudo apt-get upgrade && sudo apt dist-upgrade && sudo apt autoremove" - elif command -v pkg > /dev/null 2>&1; then + elif command -v pkg >/dev/null 2>&1; then update_command="sudo pkg update && sudo pkg upgrade && sudo pkg autoremove" fi eval "$update_command" - command -v brew > /dev/null && echo "Brew:" && brew update && brew upgrade && brew autoremove && brew cleanup - command -v flatpak > /dev/null && echo "Flatpak:" && flatpak update && flatpak uninstall --unused && flatpak --user uninstall --unused && flatpak repair - command -v npm > /dev/null && npm update -g + command -v brew >/dev/null && echo "Brew:" && brew update && brew upgrade && brew autoremove && brew cleanup + command -v flatpak >/dev/null && echo "Flatpak:" && flatpak update && flatpak uninstall --unused && flatpak --user uninstall --unused && flatpak repair + command -v npm >/dev/null && npm update -g command -v rustup >/dev/null && rustup update } # Update & Upgrades -autorem(){ +autorem() { remove_command="" - if command -v apt-get > /dev/null 2>&1; then + if command -v apt-get >/dev/null 2>&1; then remove_command="sudo apt autoremove" - elif command -v dnf > /dev/null 2>&1; then + elif command -v dnf >/dev/null 2>&1; then remove_command="sudo dnf autoremove" - elif command -v pkg > /dev/null 2>&1; then + elif command -v pkg >/dev/null 2>&1; then remove_command="sudo pkg autoremove" fi @@ -63,10 +114,10 @@ install() { return 1 fi - if command -v apt-get > /dev/null 2>&1; then + if command -v apt-get >/dev/null 2>&1; then sudo apt-get install "$1" os_install_status=$? - elif command -v dnf > /dev/null 2>&1; then + elif command -v dnf >/dev/null 2>&1; then sudo dnf install "$1" os_install_status=$? else @@ -122,84 +173,3 @@ alias dnscheck="dnscrypt-proxy -resolve google.com" alias ips='printf "Local IP:- "; hostname -I | cut -f1 -d " "; printf "Public IP:- "; curl -s https://ipinfo.io/ip' alias ipdetails='printf "Local IP:- "; hostname -I | cut -f1 -d " "; printf "Public IP Details:- \n"; geoip' alias listening_apps="sudo netstat -nutlp | grep ':' | awk '{print \$1,\$4,\$NF}' | awk -F: '{print \$1,\$(NF-1),\$NF}' | awk -v OFS=\"\t\" 'BEGIN {printf (\"%s\t%s\t\t%s \n\", \"PROTO\", \"PORT\", \"APPLICATION\")} {print \$1 , \$(NF-1) ,\" \" , \$NF}' | (read -r; printf \"%s\n\" \"\$REPLY\"; sort -k2 -n)" - -# For servers -alias ngt="sudo nginx -t" -alias ngrestart="sudo systemctl restart nginx" -alias ngreload="sudo systemctl reload nginx" -alias ngstop="sudo systemctl stop nginx" - -f2b_banned_ips() { - provided_jail=$1 - - if [ "${provided_jail// /}" != "" ]; then - for ip in "$(sudo fail2ban-client status "$provided_jail" | tail -1 | sed 's/[^:]*://;s/\s*//')" - do - printf "%17s\n" "$ip" - done - else - total_ips_banned=0 - for JAIL in "$(sudo fail2ban-client status | tail -1 | sed 's/[^:]*://;s/\s*//;s/,//g')" - do - banned_ip_count=$(sudo fail2ban-client status "$JAIL" | grep -oP 'Currently banned:\s*\K\d+') - - if [ "$banned_ip_count" -gt 0 ]; then - echo "${JAIL}: ${banned_ip_count}" - - for ip in "$(sudo fail2ban-client status "$JAIL" | tail -1 | sed 's/[^:]*://;s/\s*//')" - do - printf "%17s\n" "[$ip]" - done - - total_ips_banned=$(( total_ips_banned + banned_ip_count )) - - echo - else - echo -e "${JAIL}:\n -None-\n" - fi - done - - echo "Total IPs banned across all jails - ${total_ips_banned}" - fi -} - -f2b_unban_ip() { - ip_to_unban="$1" - jail="$2" - - # If jail is provided - use that jail to directly unban - if [ "${jail// /}" != "" ]; then - sudo fail2ban-client set "$jail" unbanip "$ip_to_unban" > /dev/null && echo "Successfully released ban" - else - # Find all JAILS this IP belong to - # Unban the ip where ever it is found - - for JAIL in "$(sudo fail2ban-client status | tail -1 | sed 's/[^:]*://;s/\s*//;s/,//g')" - do - banned_ip_count=$(sudo fail2ban-client status "$JAIL" | grep -oP 'Currently banned:\s*\K\d+') - if [ "$banned_ip_count" -gt 0 ] && [[ $(sudo fail2ban-client status "$JAIL") == *"$ip_to_unban"* ]]; then - found_ip="true" - echo "Unbanning from ${JAIL}:" - sudo fail2ban-client set "$JAIL" unbanip "$ip_to_unban" > /dev/null && echo "Successfully released ban" - fi - done - - if [[ -z "${found_ip// /}" ]]; then - echo "${ip_to_unban} was not found in any banned lists." - echo "No action taken." - fi - fi -} - -f2b_ban_an_ip(){ - ip_to_ban=$1 - ban_jail=$2 - - if [[ ( -z "${ip_to_ban// /}" ) || ( -z "${ban_jail// /}" ) ]]; then - echo "Please provide an IP and a Jail (in that order)" - echo -e "eg -\n\t f2b_ban_an_ip 1.1.1.1 sshd" - return 1 - fi - - sudo fail2ban-client set "$ban_jail" banip "$ip_to_ban" > /dev/null && echo "Ban successful" -}