Home  â€º  Articles

Terminal Commands Worth Knowing for Mac Maintenance

Updated 2026-07-26  Â·  3 min read

Most Mac maintenance advice involving Terminal is either outdated or harmful. These are the ones that earn their place, split into commands that tell you something and commands that change something.

Commands that tell you something

Start here. Diagnosis before action saves you from deleting the wrong thing.

Real free space

df -h /

The Avail column is free space, unlike Finder which counts purgeable space as available. When an installer insists there's no room and Finder disagrees, this is the number that's telling the truth.

What's eating a folder

du -sh ~/Library/* | sort -h | tail -20

Sizes every item in your Library and shows the twenty largest. This one command finds more reclaimable space than any other on this page. Expect Caches, Application Support and Containers near the top.

Time Machine's local snapshots

tmutil listlocalsnapshots /

Lists snapshots stored on your internal drive. These are frequently the reason free space and reported space disagree by tens of gigabytes.

What's talking to the network

lsof -i -P | grep ESTABLISHED

Every process with an open connection right now, with the remote address. Useful when something is using bandwidth and you'd like to know what.

Memory pressure, honestly

vm_stat 5

Updates every five seconds. Watch "Pages free" and the compressor figures. Activity Monitor's graph is easier to read, but this works over SSH and in Recovery.

Commands that change something

Everything here's safe, in the sense that it removes data macOS or your tools will recreate. Quit the relevant applications first.

Flush the DNS cache

sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder

Fixes a category of problem: a site that resolves to the wrong address after a DNS change. It won't speed anything up otherwise.

Reclaim snapshot space

sudo tmutil thinlocalsnapshots / 10000000000 4

Asks macOS to free roughly 10 GB of snapshots at the most aggressive urgency level. macOS normally handles this itself, so reach for it when a large operation is failing for lack of space.

Rebuild the Spotlight index

sudo mdutil -E /

Erases and rebuilds the index. Fixes Spotlight returning nothing or returning nonsense. Expect heavy disk activity for a while afterwards, so don't run it ten minutes before you need the machine.

Clear developer caches

npm cache clean --force
brew cleanup --prune=all
xcrun simctl delete unavailable

Three of the highest-yield commands on a development machine. The full picture is in deleting Xcode DerivedData.

Run the periodic maintenance scripts

sudo periodic daily weekly monthly

These rotate logs and tidy temporary files. macOS runs them on a schedule already, so this matters mainly on a Mac that's usually asleep at the scheduled times. The benefit is modest and honest tools say so.

Two habits worth keeping Read any command you're pasting from the internet before you run it, particularly anything with sudo rm -rf in it. And prefer moving files to the Trash over deleting them, because a recoverable mistake isn't really a mistake.

Commands to skip

sudo purge forces macOS to drop its disk cache. The memory graph looks better and the Mac gets slower, because everything it dropped has to be read from disk again. It's the command-line version of a memory cleaner app.

Repairing permissions. Every guide older than about 2015 recommends it. System Integrity Protection made it obsolete, and the command was removed. If you find advice telling you to run it, the rest of that page is probably out of date too.

sudo rm -rf ~/Library/Caches/* works, but it deletes rather than trashes, it runs as root for no reason, and one mistyped character in that path is unrecoverable. Emptying the folders through Finder achieves the same thing with an undo.

Disabling SIP. Occasionally suggested for cleaning system files. The space you'd recover is small and the protection you'd lose is significant.

Skip the Terminal entirely

Everything on this page that's worth doing, CleanMachine does with a scan and a confirmation, including developer caches across seven toolchains and snapshot management. Everything removed goes to a recoverable Trash. Free to scan.

↓ Download Free & Scan

A note on sudo

Only three commands here need it, and each one has a reason: flushing DNS touches a system daemon, thinning snapshots modifies system state, and rebuilding the Spotlight index writes outside your home folder.

If a maintenance guide asks for sudo on something inside your own home directory, it's usually a sign the author didn't understand why. You own those files already.

Common questions

What Terminal commands help clean up a Mac?
du -sh ~/Library/* | sort -h finds what's using space, df -h / shows free space, tmutil listlocalsnapshots reveals snapshot usage, and npm cache clean, brew cleanup and xcrun simctl delete unavailable clear developer caches.
Does sudo purge free up memory on a Mac?
It drops the disk cache, so the graph improves while the Mac gets slower, because that data has to be read back from disk. macOS manages memory well on its own and the command is best avoided.
How do I flush the DNS cache on a Mac?
Run sudo dscacheutil -flushcache followed by sudo killall -HUP mDNSResponder. It fixes sites resolving to stale addresses but won't improve speed generally.
Is repairing disk permissions still needed?
No. System Integrity Protection made it obsolete and Apple removed the command. Any guide still recommending it is out of date.

Keep reading