macOS Terminal Power Tips: Hidden Commands Every Mac User Should Know

macOS Terminal Power Tips: Hidden Commands Every Mac User Should Know


macOS comes with a powerful Unix terminal, but most users barely scratch the surface. Beyond standard Linux commands, macOS has unique tools like defaults, open, pbcopy, caffeinate, and mdfind that let you control almost everything from the command line.

This guide covers the most useful macOS-specific terminal commands for system configuration, file management, networking, and productivity.

Clipboard: pbcopy and pbpaste

macOS has built-in clipboard commands that work with pipes:

# Copy file contents to clipboard
pbcopy < ~/.ssh/id_ed25519.pub

# Copy command output to clipboard
echo "Hello" | pbcopy
cat file.txt | pbcopy
pwd | pbcopy

# Paste from clipboard to a file
pbpaste > output.txt

# Paste and pipe to another command
pbpaste | grep "search term"
pbpaste | wc -l

# Copy your IP address
curl -s ifconfig.me | pbcopy

These are incredibly useful for copying SSH keys, sharing command output, and moving text between terminal and GUI apps.

Opening Files and Apps: open

The open command bridges terminal and GUI:

# Open current directory in Finder
open .

# Open a file with its default application
open document.pdf
open image.png
open webpage.html

# Open a URL in the default browser
open https://example.com

# Open with a specific application
open -a "Google Chrome" index.html
open -a "Visual Studio Code" .
open -a Preview screenshot.png

# Open a new Finder window at a specific path
open /usr/local/bin

# Reveal a file in Finder
open -R ~/Downloads/file.zip

Open with VS Code

# Open current directory in VS Code
code .

# If 'code' isn't available, use open
open -a "Visual Studio Code" .

# Open a specific file
open -a "Visual Studio Code" ~/.zshrc

System Defaults: defaults

The defaults command reads and writes macOS system preferences — many of which aren’t available in System Settings:

Finder Tweaks

# Show hidden files in Finder
defaults write com.apple.finder AppleShowAllFiles -bool true
killall Finder

# Show file extensions
defaults write NSGlobalDomain AppleShowAllExtensions -bool true
killall Finder

# Show path bar in Finder
defaults write com.apple.finder ShowPathbar -bool true
killall Finder

# Show full POSIX path in Finder title bar
defaults write com.apple.finder _FXShowPosixPathInTitle -bool true
killall Finder

# Disable .DS_Store files on network and USB drives
defaults write com.apple.desktopservices DSDontWriteNetworkStores -bool true
defaults write com.apple.desktopservices DSDontWriteUSBStores -bool true

# Default to list view in Finder
defaults write com.apple.finder FXPreferredViewStyle -string "Nlsv"
killall Finder

Dock Tweaks

# Auto-hide the Dock
defaults write com.apple.dock autohide -bool true
killall Dock

# Remove Dock auto-hide delay
defaults write com.apple.dock autohide-delay -float 0
defaults write com.apple.dock autohide-time-modifier -float 0.3
killall Dock

# Set Dock icon size
defaults write com.apple.dock tilesize -int 48
killall Dock

# Add a spacer tile to the Dock
defaults write com.apple.dock persistent-apps -array-add '{"tile-type"="spacer-tile";}'
killall Dock

# Don't show recent apps in Dock
defaults write com.apple.dock show-recents -bool false
killall Dock

# Minimize windows to application icon
defaults write com.apple.dock minimize-to-application -bool true
killall Dock

Screenshots

# Change screenshot save location
defaults write com.apple.screencapture location ~/Pictures/Screenshots
killall SystemUIServer

# Change screenshot format (png, jpg, pdf, tiff)
defaults write com.apple.screencapture type -string "png"

# Disable screenshot shadow
defaults write com.apple.screencapture disable-shadow -bool true
killall SystemUIServer

# Change default screenshot name
defaults write com.apple.screencapture name "Screenshot"

Other System Tweaks

# Disable auto-correct
defaults write NSGlobalDomain NSAutomaticSpellingCorrectionEnabled -bool false

# Enable key repeat (instead of accent menu)
defaults write NSGlobalDomain ApplePressAndHoldEnabled -bool false

# Set fast key repeat rate
defaults write NSGlobalDomain KeyRepeat -int 2
defaults write NSGlobalDomain InitialKeyRepeat -int 15

# Expand save panel by default
defaults write NSGlobalDomain NSNavPanelExpandedStateForSaveMode -bool true

# Disable "Are you sure you want to open this application?"
defaults write com.apple.LaunchServices LSQuarantine -bool false

Read Current Defaults

# Read a specific default
defaults read com.apple.dock autohide

# Read all defaults for an app
defaults read com.apple.finder

# Find all defaults containing a keyword
defaults read | grep -i "dock"

Prevent Sleep: caffeinate

Keep your Mac awake:

# Prevent sleep indefinitely (until Ctrl+C)
caffeinate

# Prevent sleep for 1 hour (3600 seconds)
caffeinate -t 3600

# Prevent sleep while a command runs
caffeinate -s make build

# Prevent display sleep
caffeinate -d

# Prevent disk sleep
caffeinate -m

Spotlight Search: mdfind

mdfind is Spotlight from the command line — faster than find for indexed content:

# Search by filename
mdfind -name "report.pdf"

# Search by content (full text search)
mdfind "project deadline"

# Search only in a specific directory
mdfind -onlyin ~/Documents "budget"

# Find all PDFs
mdfind "kMDItemKind == 'PDF'"

# Find files modified today
mdfind "kMDItemFSContentChangeDate >= $time.today"

# Find files larger than 1GB
mdfind "kMDItemFSSize > 1073741824"

# Find images
mdfind "kMDItemContentTypeTree == 'public.image'"

# Find by file extension
mdfind "kMDItemFSName == '*.py'"

Networking Commands

Check Your IP and Network

# Local IP address
ipconfig getifaddr en0   # Wi-Fi
ipconfig getifaddr en1   # Ethernet

# Public IP address
curl -s ifconfig.me

# DNS information
scutil --dns | grep nameserver

# Change DNS servers
sudo networksetup -setdnsservers Wi-Fi 1.1.1.1 1.0.0.1

# Reset DNS to DHCP default
sudo networksetup -setdnsservers Wi-Fi Empty

# List all network interfaces
networksetup -listallhardwareports

# Check Wi-Fi status
/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I

# Flush DNS cache
sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder

Wi-Fi Management

# Scan for Wi-Fi networks
/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -s

# Current Wi-Fi network name
networksetup -getairportnetwork en0

# Turn Wi-Fi off/on
networksetup -setairportpower en0 off
networksetup -setairportpower en0 on

# Connect to a Wi-Fi network
networksetup -setairportnetwork en0 "NetworkName" "Password"

File Management

Quick Look from Terminal

# Preview a file with Quick Look
qlmanage -p document.pdf
qlmanage -p image.png

Disk and Volume Info

# List all mounted volumes
ls /Volumes/

# Disk usage
diskutil list
diskutil info /

# Eject a volume
diskutil eject /Volumes/USBDrive

# Get disk space in human-readable format
df -h

File Attributes

# Show extended attributes
xattr -l file.txt

# Remove quarantine attribute (for downloaded apps)
xattr -d com.apple.quarantine /Applications/SomeApp.app

# Remove all extended attributes
xattr -c file.txt

System Information

# macOS version
sw_vers

# Hardware overview
system_profiler SPHardwareDataType

# Battery status (laptops)
pmset -g batt

# CPU info
sysctl -n machdep.cpu.brand_string

# Memory info
sysctl hw.memsize

# Uptime
uptime

# List running processes (sorted by CPU)
top -o cpu -n 10

# Kill an app by name
killall "App Name"

# Force quit an app
killall -9 "App Name"

Useful Aliases for macOS

Add these to ~/.zshrc:

# Quick navigation
alias dl="cd ~/Downloads"
alias dt="cd ~/Desktop"
alias doc="cd ~/Documents"

# Clipboard
alias cpwd='pwd | pbcopy'
alias clip='pbcopy'
alias paste='pbpaste'

# IP addresses
alias localip="ipconfig getifaddr en0"
alias publicip="curl -s ifconfig.me"

# macOS specific
alias showfiles="defaults write com.apple.finder AppleShowAllFiles -bool true && killall Finder"
alias hidefiles="defaults write com.apple.finder AppleShowAllFiles -bool false && killall Finder"
alias flushdns="sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder"
alias cleanup="find . -type f -name '.DS_Store' -delete"

# Lock screen
alias lock="/System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -suspend"

# Empty trash
alias emptytrash="sudo rm -rfv /Volumes/*/.Trashes; sudo rm -rfv ~/.Trash"

# Update everything
alias update="brew update && brew upgrade && brew cleanup"

Summary

macOS’s terminal is more powerful than most users realize. Commands like defaults, open, pbcopy, caffeinate, and mdfind give you control over virtually every aspect of the system.

Key resources:

Master these commands and you’ll use your Mac more efficiently than anyone clicking through System Settings.