Skip to content
10 Actually Useful Bash Functions (2026)

10 Actually Useful Bash Functions (2026)

DodaTech Updated Jun 20, 2026 3 min read

Most .bashrc files have aliases for ls, grep, and ... Fine, but they save seconds. The functions here save minutes — automating archive extraction, killing processes by port, cleaning up Docker, and formatting JSON. Drop these into your .bashrc or .zshrc and they pay for themselves within the first week.

The Functions

mkcd — Creates a directory and changes into it in one command. Eliminates the two-step mkdir then cd dance that every developer does dozens of times per day.

mkcd() { mkdir -p "$1" && cd "$1"; }

extract — Detects the archive type by extension and extracts it with the right command. No more Googling “how to extract .tar.bz2”.

extract() {
  if [ -f "$1" ]; then
    case "$1" in
      *.tar.gz|*.tgz) tar -xzf "$1" ;;
      *.tar.bz2)     tar -xjf "$1" ;;
      *.zip)         unzip "$1" ;;
      *.rar)         unrar x "$1" ;;
      *.7z)          7z x "$1" ;;
      *)             echo "Unknown archive type" ;;
    esac
  else
    echo "File not found"
  fi
}

killport — Finds and kills the process listening on a given port. The most-used debugging function for anyone running local dev servers.

killport() {
  local pid=$(lsof -ti:$1)
  if [ -n "$pid" ]; then
    kill -9 $pid && echo "Killed process $pid on port $1"
  else
    echo "Nothing running on port $1"
  fi
}

weather — Shows a terminal weather forecast for your city using wttr.in. No API keys, no signup, just curl.

weather() { curl -s "wttr.in/${1:-London}?format=3"; }
# Usage: weather "New York" or just weather

cheat — Quick reference for any command using cheat.sh. Provides concise examples instead of wall-of-text man pages.

cheat() { curl -s "cheat.sh/$1"; }
# Usage: cheat tar or cheat find

google — Searches Google from the command line and opens the first result. For when you don’t want to leave the terminal.

google() { open "https://google.com/search?q=${*// /+}"; }
# Linux: replace 'open' with 'xdg-open'

docker-clean — Prunes all unused Docker resources. Run this weekly to reclaim gigabytes of disk space from orphaned containers and dangling images.

docker-clean() {
  docker system prune -af --volumes
  echo "Docker cleaned."
}

tree — Enhanced directory listing with depth control, hidden files, and directory-only options.

tree() {
  local opts="--tree --level=2"
  [ "$1" = "-a" ] && opts="$opts --all" && shift
  [ -n "$1" ] && opts="$opts --level=$1"
  exa $opts
}
# Falls back to: find . -maxdepth 2 | sed 's|[^/]*/|  |g' | sort

json — Pretty-prints JSON with syntax highlighting. Pipes from curl or reads from a file.

json() {
  if [ -p /dev/stdin ]; then
    cat - | python3 -m json.tool | pygmentize -l json
  elif [ -f "$1" ]; then
    python3 -m json.tool "$1" | pygmentize -l json
  else
    echo "$1" | python3 -m json.tool | pygmentize -l json
  fi
}
# Usage: curl api.example.com | json

timer — A simple stopwatch that displays elapsed time in hours:minutes:seconds. Counts up until you press Ctrl+C.

timer() {
  local start=$SECONDS
  while true; do
    local elapsed=$((SECONDS - start))
    printf '\rElapsed: %02d:%02d:%02d' $((elapsed/3600)) $(((elapsed%3600)/60)) $((elapsed%60))
    sleep 1
  done
}
Where do I put these functions?
Add them to ~/.bashrc, ~/.zshrc, or ~/.bash_profile. Or create a separate file like ~/.bash_functions and source it from your rc file: source ~/.bash_functions. This keeps your rc file clean and organized.
Do I need any dependencies?
mkcd and timer use only built-in shell commands. extract needs unzip, unrar, or 7z depending on formats. killport needs lsof. json needs python3 and optionally pygmentize. tree optionally uses exa (install via brew install exa or apt install exa).
Will these work on macOS?
Yes. lsof is built into macOS. python3 comes with macOS or via Homebrew. Replace xdg-open with open for the google function. The only difference is GNU vs BSD flags for some commands — test before relying on them in scripts.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro