Skip to main content
coreydaley.dev
Docs menu

Everyday Mac command-line tools

Small tools for things you do every day, all built into macOS. I ran each example on macOS 27.2; output is trimmed.

mdfind and mdls: Spotlight from Terminal

mdfind searches the same index as Spotlight, including the text inside Word, RTF and other documents.

mdfind -onlyin ~/Documents "quarterly report" 2>/dev/null   # text inside files
mdfind -onlyin ~/Documents -name invoice 2>/dev/null        # file names
mdfind -onlyin ~/Pictures 'kMDItemPixelWidth > 3000' 2>/dev/null
mdfind -onlyin ~/Documents -count "kind:image" 2>/dev/null

-onlyin limits the search to a folder; without it, mdfind searches the whole Mac. On macOS 27.2 every search also printed a “[UserQueryParser]” line to the error output, which 2>/dev/null hides. A file I had just made was found within a few seconds.

mdls shows the Spotlight details of one file, and -raw gives a bare value for scripts:

mdls -name kMDItemKind -name kMDItemPixelWidth photo.jpg
mdls -raw -name kMDItemPixelWidth photo.jpg

pbcopy and pbpaste: the clipboard

pbcopy < notes.txt                    # copy a file's text
pbpaste | tr a-z A-Z | pbcopy         # uppercase whatever is on the clipboard
ls | pbcopy                           # copy a file list to paste anywhere

These handle text only; an image on the clipboard doesn’t come through pbpaste.

caffeinate: keep the Mac awake

caffeinate -i ./long-backup.sh        # awake until the script finishes
caffeinate -i -t 3600                 # awake for an hour

-i stops idle sleep for as long as the command runs, then lets go, and passes through the command’s exit status. -d keeps the display on too, and -s prevents system sleep only while the Mac is on power.

say: speak, or make an audio file

say "Backup finished"
say -o finished.aiff "Backup finished"           # to a file, silently
say -o finished.m4a --data-format=aac "Backup finished"
say -f notes.txt -o notes.aiff                   # a text file, to audio
say -v '?'                                       # list voices

With -o, nothing plays; I only tested the -o forms. say -v '?' lists the installed voices, over 180 on this Mac.

screencapture: screenshots from a script

screencapture -x -R 0,0,800,600 corner.png       # a region, in points, no sound
screencapture -x shot.png                        # whole screen (not run here)
screencapture -c                                 # to the clipboard (not run here)

-R is in points, so on a Retina display a 40×40 region saved as 80×80 pixels. Terminal needs Screen Recording permission in System Settings > Privacy & Security; without it you may get only the wallpaper.

networkQuality: test your connection

networkQuality -s

It tests against Apple’s servers and reports upload and download speed, and responsiveness: how quickly the connection answers while it’s busy, in round-trips per minute (RPM), higher being better. My run took about half a minute:

Uplink capacity: 247.134 Mbps
Downlink capacity: 454.241 Mbps
Uplink Responsiveness: Medium (245.741 milliseconds | 244 RPM)
Downlink Responsiveness: Medium (111.156 milliseconds | 539 RPM)
Idle Latency: 30.051 milliseconds | 1996 RPM

It uses real data, so think twice on a metered connection. -s runs upload and download one after the other; without it they run together.