Terminal Commands Every Mac User Should Know
Most Terminal guides teach you Unix. This is the other half: the commands Apple added, which do things the interface cannot, and which answer questions you would otherwise guess at. None of them change anything unless you tell them to.
Keep the Mac awake
# stay awake until you press Control-C
caffeinate -d
# stay awake for exactly one hour
caffeinate -d -t 3600
# stay awake only while a command runs
caffeinate -i ./long-build.sh
That last form is the best of the three. The Mac sleeps normally again the moment the job finishes, with nothing to remember to switch off.
Answer power questions
# what is preventing sleep right now, and which process
pmset -g assertions
# current sleep, display and wake settings
pmset -g custom
# why did it wake? read the log
pmset -g log | grep "Wake from" | tail -10
The first one answers "why won't my Mac sleep" faster than any troubleshooting article, including this site's.
Search better than Spotlight's window
# Spotlight search from the command line
mdfind "quarterly report"
# search only inside one folder
mdfind -onlyin ~/Documents "invoice"
# every piece of metadata Spotlight holds about a file
mdls somefile.pdf
mdls is the surprising one: it shows dimensions, durations, authorship, where a download came from and much more, all without opening the file.
Find where your disk went
# biggest items in the current folder
du -sh * | sort -h
# free space, in human terms
df -h /
# is Spotlight indexing right now?
mdutil -s /
Files, the Mac way
# open a file or folder in the right app
open report.pdf
open .
# reveal a file in Finder
open -R somefile.txt
# pipe text straight into the clipboard, and back out
cat notes.txt | pbcopy
pbpaste > copy.txt
open . — open the current folder in Finder — is the most-used command on this page once you know it. pbcopy and pbpaste bridge the Terminal and everything else on the Mac.
The genuinely macOS-only ones
# take a screenshot from the command line, with a 5 second delay
screencapture -T 5 ~/Desktop/shot.png
# make the Mac speak
say "the build has finished"
# useful at the end of a long job
./deploy.sh && say "deploy complete"
# hardware, memory, graphics, everything
system_profiler SPHardwareDataType
Look inside files and apps
# what kind of file is this, really?
file mystery-download
# where did this file come from?
xattr -l ~/Downloads/installer.dmg
# is an app signed, and by whom?
codesign -dv --verbose=2 /Applications/Some.app 2>&1 | grep Authority
# has it been notarised by Apple?
spctl -a -vv /Applications/Some.app
The last two are the practical way to check that an app you downloaded is what it claims to be. A valid Developer ID authority and a "notarized developer" verdict mean Apple has checked it, and both take a second to run.
Two habits worth forming
Read before you write. Every one of these is safe. The moment you move to commands that change things, read the current value first so you know what to restore.
Use man. Every command here has a manual page: man pmset, man caffeinate, man mdfind. Press Space to page down, q to quit, and /word to search. It is the single most useful habit in a terminal, and it is entirely offline.
For the settings side of this list, Mainspring turns 90+ hidden macOS preferences into labelled toggles, each reversible from the same switch that applied it.
Try Mainspring free →Signed & notarized by Apple · 1-day free trial · $29 once