My Zsh and Bash Aliases

Git In The Shell

I keep my aliases/functions in a Git repo so that they’re both portable and versioned.

I use zsh as my daily driver, but also bash from time to time. Aliases for both are included below. I hope you’ll find something you can copy for your own enhanced laziness efficiency.

Zsh Aliases

# If on WSL then 'exec ssh-agent zsh' must be run prior to using 'agent'
agent() { ssh-add ~/.ssh/[email protected]"[email protected]"_ed25519; }

# Must be run in WSL prior to running 'agent()'
alias wsh="exec ssh-agent zsh"

# Because install == install
getget() { apt-get install -y "[email protected]"; }

# Let's leave 'rm' alone safely in it's natural habitat
trash() { mv --force $1 ~/.trash; }

# How CLI peasants use 'find' (for fuzzy directory search, use 'locate' instead)
findf() { find -iname "*""[email protected]""*" -type f; }

# Quickly see what's running on a certain port
listen() { lsof -i :"[email protected]" | grep LISTEN; }

# Send files using 'https://transfer.sh'
transfer() { if [ $# -eq 0 ]; then echo -e "No arguments specified. Usage:\necho transfer /tmp/test.md\ncat /tmp/test.md | transfer test.md"; return 1; fi
tmpfile=$( mktemp -t transferXXX ); if tty -s; then basefile=$(basename "$1" | sed -e 's/[^a-zA-Z0-9._-]/-/g'); curl --progress-bar --upload-file "$1" "https://transfer.sh/$basefile" >> $tmpfile; else curl --progress-bar --upload-file "-" "https://transfer.sh/$1" >> $tmpfile ; fi; cat $tmpfile; rm -f $tmpfile; } 

# My fingers are too lazy to entertain tmux' syntax
alias tmk="tmux kill-ses -t"
alias tmn="tmux new -s"
alias tma="tmux a -t"

# Batman Catwoman
alias cat="bat"

# Get IP address
alias ipaddy="curl ipaddy.net"

# Road warrior VPN
alias sshtun="ssh -D 8888"

# ZeroTier
alias zt="zerotier-cli"

# WireGuard
alias wg1="wg-quick up"
alias wg0="wg-quick down"

# Backblaze B2
alias b2sync="backblaze-b2 sync --threads 4"
alias b2syncdry="backblaze-b2 sync --threads 4 --dryRun"
alias b2acc="backblaze-b2 get-account-info"

# One-two combo
alias cls="clear && ls"

# Ease of use (also, laziness)
alias date="cal && date"

# Because zip/unzip makes sense (thanks, English)
alias untar="tar -xvzf"

# Assume case insensitivity when searching
alias locate="locate -i"

# Create parent directory implicitly
alias mkdir="mkdir -pv"

# Pragmatism, anyone?
alias ls="ls -ah"
alias lsv="ls -lah"
alias lss="ls --human-readable --size -1 -S --classify"

# Show actual size of current dir or file
alias size="du -sh --apparent-size"

# Show what's mounted where
alias mnt="mount | grep -E ^/dev | column -t"

# Force nuking of non-empty directories
alias rmdir="rm -rf"

# Put a (non-BDSM) leash on ping
alias ping="ping -c 5"

# Assume netstat is for port-checking
alias netstat="netstat -tulanp"

# There is no 'i' in the word 'head'
alias curlhead="curl -I"

# Count the number of lines in a file
alias numlines="wc -l"

# Full package update
alias update="apt-get update && apt-get upgrade -y"

# Show simplified CPU usage
alias cpu="ps auxf | sort -nr -k 3 | head -10"

# Show simplified Disk usage
alias disk="df -hl --total | grep total"

# Show simplified RAM usage
alias ram="free -m"

# Find PID by process name, before slaughtering it with the 'kill' command
alias procid="ps ax | grep"

Bash Aliases

urldecode() {
    # Usage: urldecode "string"
    : "${1//+/ }"
    printf '%b\n' "${_//%/\\x}"
}
# Example Usage:
# $ urldecode "https%3A%2F%2Fgithub.com%2Fdylanaraps%2Fpure-bash-bible"
# https://github.com/dylanaraps/pure-bash-bible

cathead() {
    # Usage: cathead "n" "file"
    mapfile -tn "$1" line < "$2"
    printf '%s\n' "${line[@]}"
}
# $ cathead 2 ~/.bashrc
# # ~/.bashrc: executed by bash(1) for non-login shells.
# # see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)

cattail() {
    # Usage: cattail "n" "file"
    mapfile -tn 0 line < "$2"
    printf '%s\n' "${line[@]: -$1}"
}
# $ cattail 3 ~/.bashrc
#    . /etc/bash_completion
#  fi
# fi

trim_whitespaces() {
    # Usage: trim_whitespaces "   example   string    "
    set -f
    set -- $*
    printf '%s\n' "$*"
    set +f
}
# $ trim_whitespaces "    Hello,    World    "
# Hello, World

split() {
   # Usage: split "string" "delimiter"
   IFS=$'\n' read -d "" -ra arr <<< "${1//$2/$'\n'}"
   printf '%s\n' "${arr[@]}"
}
# $ split "apples,oranges,pears,grapes" ","
# apples
# oranges
# pears
# grapes
# 
# $ split "hello---world---my---name---is---john" "---"
# hello
# world
# my
# name
# is
# john

lowercase() {
    # Usage: lowercase "string"
    printf '%s\n' "${1,,}"
}
# $ lowercase "HELLO"
# hello
# 
# $ lowercase "HeLlO"
# hello
# 
# $ lowercase "hello"
# hello

uppercase() {
    # Usage: uppercase "string"
    printf '%s\n' "${1^^}"
}
# $ uppercase "hello"
# HELLO
# 
# $ uppercase "HeLlO"
# HELLO
# 
# $ uppercase "HELLO"
# HELLO

strip_pattern() {
    # Usage: strip_pattern "string" "pattern"
    printf '%s\n' "${1//$2}"
}
# $ strip_pattern "The Quick Brown Fox" "[aeiou]"
# Th Qck Brwn Fx
# 
# $ strip_pattern "The Quick Brown Fox" "[[:space:]]"
# TheQuickBrownFox
# 
# $ strip_pattern "The Quick Brown Fox" "Quick "
# The Brown Fox

dedupe() {
    # Usage: dedupe "array"
    declare -A tmp_array

    for i in "[email protected]"; do
        [[ $i ]] && IFS=" " tmp_array["${i:- }"]=1
    done

    printf '%s\n' "${!tmp_array[@]}"
}
# $ dedupe 1 1 2 2 3 3 3 3 3 4 4 4 4 4 5 5 5 5 5 5
# 1
# 2
# 3
# 4
# 5
# 
# arr=(red red green blue blue)
# $ dedupe "${arr[@]}"
# red
# green
# blue

filecount() {
    # Usage: filecount /path/to/dir/*
    #        filecount /path/to/dir/*/
    printf '%s\n' "$#"
}
# // Count all files in dir
# $ count ~/Downloads/*
# 232
# 
# // Count all dirs in dir
# $ count ~/Downloads/*/
# 45
# 
# // Count all jpg files in dir
# $ count ~/Pictures/*.jpg
# 64

extract() {
    # Usage: extract file "opening marker" "closing marker"
    while IFS=$'\n' read -r line; do
        [[ $extract && $line != "$3" ]] &&
            printf '%s\n' "$line"

        [[ $line == "$2" ]] && extract=1
        [[ $line == "$3" ]] && extract=
    done < "$1"
}
# // Extract code blocks from MarkDown file
# $ extract ~/projects/pure-bash/README.md '```sh' '```'
# Output here...

Merging Changes

Storing dotfiles in their own folder is a best practice in my opinion. To update the actual files after making changes (especially across machines), I use a shell function alia() — to copy configuration files to their actual location after doing a git pull on the repo.

The alia function looks like this:

alia() {  
    cp /home/paramdeo/GitHub/dotfiles/.zshrc ~/.zshrc;  
    cp /home/paramdeo/GitHub/dotfiles/.bash_aliases ~/.bash_aliases;  
}

That is all. Have a nice day.