Developers who move to a Mac usually bring their Linux habits and miss the commands that only exist on macOS. Those commands bridge the shell and the desktop: piping into the clipboard, opening files in the right app, searching with Spotlight, scheduling background jobs the way the system expects, and reading logs that never reach a text file.

Everything here works on current macOS releases (macOS 15 Sequoia and macOS 26 Tahoe) with the default zsh shell. Run man <command> for the full options; the manual pages ship with the system.

Clipboard: pbcopy and pbpaste

pbcopy < ~/.ssh/id_ed25519.pub          # copy a public key
git diff | pbcopy                        # copy a diff to paste into a review
pbpaste | jq .                           # pretty-print JSON you copied
pbpaste | sort -u | pbcopy               # dedupe the clipboard in place

These read and write the general pasteboard as plain text. They make the clipboard a normal Unix pipe stage, which is surprisingly powerful for one-off cleanups.

Opening things: open

open .                          # current folder in Finder
open -R build/app.zip           # reveal a file in Finder
open https://example.com        # default browser
open -a "Visual Studio Code" .  # specific app
open -e notes.txt               # TextEdit
open -n -a Firefox --args --private-window   # new instance with arguments

open uses Launch Services, the same mechanism as double-clicking, so file associations apply. -a takes an application name; -b takes a bundle identifier such as com.apple.Safari when names are ambiguous.

Searching: mdfind and mdls

Spotlight keeps an index of file names and contents. mdfind queries it from the shell and returns instantly where find would crawl the disk:

mdfind -name docker-compose.yml               # by file name
mdfind -onlyin ~/Projects "TODO remove"        # content, scoped to a folder
mdfind "kMDItemContentType == 'com.adobe.pdf' && kMDItemFSSize > 50000000"
mdls -name kMDItemWhereFroms ~/Downloads/tool.pkg   # where a download came from

mdls prints a file's metadata attributes. kMDItemWhereFroms is handy for checking the source URL of an installer. Folders excluded in System Settings under Spotlight privacy are not indexed, so mdfind will not see them; fall back to find there.

Preferences: defaults

Most apps store settings in property lists that defaults can read and write:

defaults read com.apple.dock autohide
defaults write com.apple.screencapture location -string "$HOME/Pictures/Screenshots"
killall SystemUIServer                         # apply screenshot change
defaults write com.apple.finder AppleShowAllFiles -bool true && killall Finder
defaults delete com.apple.finder AppleShowAllFiles   # back to default
defaults domains | tr ',' '\n' | grep -i vscode

Always pass the type flag (-bool, -int, -string) so the value is stored with the type the app expects. Write commands into a setup script and you can rebuild a machine's preferences in minutes. Some settings are protected or cached and need the app restarted, or a logout, to take effect.

Background jobs: launchctl

macOS does not use cron for most scheduled work; launchd runs agents (per user) and daemons (system-wide) described by plist files. Apple's documentation describes the plist keys; the modern launchctl subcommands target a domain explicitly.

A user agent that runs a script every 30 minutes, saved as ~/Library/LaunchAgents/com.example.sync.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key><string>com.example.sync</string>
  <key>ProgramArguments</key>
  <array><string>/Users/dev/bin/sync.sh</string></array>
  <key>StartInterval</key><integer>1800</integer>
  <key>StandardOutPath</key><string>/tmp/com.example.sync.log</string>
  <key>StandardErrorPath</key><string>/tmp/com.example.sync.err</string>
</dict>
</plist>
plutil -lint ~/Library/LaunchAgents/com.example.sync.plist
launchctl bootstrap gui/$(id -u) ~/Library/LaunchAgents/com.example.sync.plist
launchctl print gui/$(id -u)/com.example.sync | head -30   # state, last exit code
launchctl kickstart -k gui/$(id -u)/com.example.sync       # run now (restart)
launchctl bootout gui/$(id -u)/com.example.sync            # unload

The older launchctl load and unload still work but are legacy. launchctl print shows the last exit status and the reason a job is not running, which is the first thing to check when a job "does nothing". Remember that launchd does not read your shell profile: PATH is minimal, so use absolute paths in scripts.

Homebrew's brew services writes and loads these plists for you for databases and other services.

Keeping the machine awake: caffeinate

caffeinate -i make release          # prevent idle sleep until the command ends
caffeinate -d -t 3600               # keep the display on for an hour
caffeinate -s -w 4821               # prevent system sleep while PID 4821 runs

Wrapping a long build, a large download or a test run in caffeinate -i stops the laptop sleeping halfway through. pmset -g assertions shows which processes currently hold sleep assertions.

Ports and processes

macOS has no ss. Use lsof with numeric output:

lsof -nP -iTCP -sTCP:LISTEN          # every listening TCP port
lsof -nP -iTCP:5432                  # what holds port 5432
kill $(lsof -t -iTCP:3000 -sTCP:LISTEN)   # stop whatever holds port 3000

Ports 5000 and 7000 are often taken by AirPlay Receiver on recent macOS versions; if a dev server cannot bind there, lsof shows ControlCe (ControlCenter), and you can either pick another port or turn AirPlay Receiver off in System Settings.

Logs: log stream and log show

Apple's unified logging system keeps most system and app messages in a binary store, not in /var/log. The log command reads it:

log stream --predicate 'process == "MyApp"' --level debug
log stream --predicate 'subsystem == "com.example.myapp" && category == "network"'
log show --last 10m --predicate 'eventMessage CONTAINS[c] "error"' --style compact
log show --last 1h --predicate 'process == "kernel" && eventMessage CONTAINS "Sandbox"'

Predicates use the same syntax as Apple's NSPredicate. Filter by subsystem and category when your app logs through os_log or Logger; they make your messages easy to isolate. The Console app shows the same data with a GUI.

System information and networking

sw_vers                                  # macOS version and build
uname -m                                 # arm64 (Apple silicon) or x86_64
system_profiler SPHardwareDataType       # model, chip, memory, serial
scutil --dns | head -40                  # resolver configuration, per-domain resolvers
networksetup -listallnetworkservices
sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder   # flush DNS cache

scutil --dns is the truth for DNS on macOS; /etc/resolv.conf exists but is not what most apps consult. VPN clients often add per-domain resolvers that only show up here.

Files, images and quarantine

xattr -l ~/Downloads/tool                 # extended attributes, including quarantine
sips -s format png input.heic --out output.png    # convert images
sips -Z 1200 photo.jpg                     # resize longest side to 1200 px
ditto -c -k --keepParent MyApp.app MyApp.zip   # zip preserving macOS metadata
diskutil list; diskutil info /

Downloads carry a com.apple.quarantine attribute that triggers Gatekeeper checks. Inspecting it is fine; removing it to bypass Gatekeeper on software you have not verified is not.

BSD versus GNU: the script traps

macOS ships BSD versions of core tools. Scripts written on Linux break in predictable ways:

Task

GNU (Linux)

BSD (macOS)

In-place edit

sed -i 's/a/b/' f

sed -i '' 's/a/b/' f

Date arithmetic

date -d '1 day ago'

date -v-1d

File modification time

stat -c %Y f

stat -f %m f

Extended regex in grep

grep -P (PCRE)

not available; use grep -E

Base64 decode

base64 -d

base64 -d (recent) or -D (older)

Readlink full path

readlink -f

realpath or readlink -f on recent releases

Either write portable scripts (POSIX options only) or install GNU tools with Homebrew (brew install coreutils gnu-sed), which installs them with a g prefix such as gsed and gdate.

Common mistakes

Mistake

Why it hurts

Do this instead

Using cron for user jobs

Missed runs during sleep, no GUI context

launchd agents with StartInterval or StartCalendarInterval

Relative paths in launchd scripts

Minimal PATH, job fails silently

Absolute paths; check launchctl print

Looking for logs in /var/log

Most logs live in the unified log

log show / log stream

defaults write without type

App reads wrong type

Use -bool, -int, -string

Linux sed -i in shared scripts

Breaks on macOS

sed -i '' or GNU gsed

Key takeaways

  • pbcopy, pbpaste and open connect the shell to the desktop.

  • mdfind searches the Spotlight index instantly; mdls reads metadata.

  • Use launchctl bootstrap, print, kickstart and bootout with a gui/<uid> domain for background jobs.

  • log stream and log show with predicates replace tailing log files.

  • Expect BSD tool differences in scripts, or install GNU tools explicitly.

References