Skip to content

Cool Bash Tricks for Your Terminal’s “Dotfiles” on macOS

You may have noticed the recent trend of techies posting their “dotfiles” on GitHub for the world to see. These usually contain shortcuts compatible with Bash terminals to automate convoluted commands that, I’ll admit, I needed to Google every single time.

My full dotfiles are posted at this Git repository, but here’s a summary of the ones I find most helpful that you can add to your own .bash_profile or .bashrc file.

Check your current IP address (IPv4 or IPv6 or both) — uses my ⚡ fast simpip server!

alias ip4="curl -4 --max-time 1 --proto-default https --silent" alias ip6="curl -6 --max-time 1 --proto-default https --silent" alias ip="ip4; ip6"

Check your current local IP address:

alias iplocal="ipconfig getifaddr en0"

Check, clear, set (Google DNS or Cloudflare DNS or custom), and flush your computer’s DNS, overriding your router:

alias dns-check="networksetup -setdnsservers Wi-Fi" alias dns-clear="networksetup -getdnsservers Wi-Fi" alias dns-set-cloudflare="dns-set" alias dns-set-google="dns-set" alias dns-set-custom="networksetup -setdnsservers Wi-Fi " # example: dns-set-custom alias dns-flush="sudo killall -HUP mDNSResponder; sudo killall mDNSResponderHelper; sudo dscacheutil -flushcache"

Start a simple local web server in current directory:

alias serve="python -c 'import SimpleHTTPServer; SimpleHTTPServer.test()'"

Test your internet connection’s speed (uses 100MB of data):

alias speed="wget -O /dev/null"

Query DNS records of a domain:

alias digg="dig @ +nocmd any +multiline +noall +answer" # example: digg

Make a new directory and change directories into it.

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

Unhide and rehide hidden files and folders on macOS:

alias unhide="defaults write AppleShowAllFiles -bool true && killall Finder" alias rehide="defaults write AppleShowAllFiles -bool false && killall Finder"

Force empty trash on macOS:

alias forcetrash="sudo rm -rf ~/.Trash /Volumes/*/.Trashes"

Quickly lock your screen on macOS:

alias afk="/System/Library/CoreServices/Menu\ Extras/ -suspend"

Update Homebrew packages, global NPM packages, Ruby Gems, and macOS in all one swoop:

alias update="brew update; brew upgrade; brew cleanup; npm install npm -g; npm update -g; sudo gem update --system; sudo gem update; sudo gem cleanup; sudo softwareupdate -i -a;"

Copy your public key to the clipboard:

alias pubkey="more ~/.ssh/ | pbcopy | echo '=> Public key copied to pasteboard.'"

Undo the most recent commit in current Git repo:

alias gundo="git push -f origin HEAD^:master"

Un-quarantine an “unidentified developer’s” application blocked by Gatekeeper on macOS’s walled prison garden:

alias unq="sudo xattr -rd"

Quickly open a Bash prompt in a running Docker container:

docker-bash() { docker exec -ti $1 /bin/bash }

Pull updates for all Docker images with the tag “latest”:

alias docker-latest="docker images --format '{{.Repository}}:{{.Tag}}' | grep :latest | xargs -L1 docker pull"

This odd hack is needed to run any of these aliases as sudo:

alias sudo="sudo "

View all of my dotfiles here or check out other cool programmers’ dotfiles over at this amazing collection.