>_ Operating systems & Linux

macOS from the Terminal: Homebrew, launchd and system tools

macOS under the hood: Terminal and zsh, installing software with Homebrew, launchd services, Activity Monitor, logs and commands unique to Apple's system.

Many people use a Mac for years without ever opening Terminal, and that's perfectly fine. But beneath the graphical interface lies a system in the Unix tradition, with a shell, a precise folder structure and commands that do in seconds what takes many steps with the mouse. Anyone who already knows Linux will feel almost at home; with a few differences worth knowing before copying commands from a guide written for another system.

Terminal and the shell

The Terminal app is in Applications > Utilities, or you can open it by searching with Spotlight. Since macOS Catalina the default shell has been zsh, which for everyday use behaves very much like Bash. Personal settings go in ~/.zshrc; variables such as PATH can also live in ~/.zprofile.

The basic commands are the ones you use on Linux: ls, cd, cp, mv, grep, find. Be careful, though: many of them come from the BSD tradition rather than GNU, and some options differ. The classic example is sed -i, which on macOS requires an argument for the backup file extension, even an empty one: sed -i '' 's/old/new/' file.txt. date and stat also have options that differ from Linux.

macOS-specific commands

Some tools exist only on the Mac and are very handy:

Command What it does
open file.pdf opens a file with the default application
open . opens the current folder in Finder
open -a Safari page.html opens a file with a specific app
pbcopy, pbpaste write to and read from the clipboard
mdfind searches using the Spotlight index
caffeinate prevents sleep while it is running
softwareupdate -l lists system updates
diskutil list lists disks and volumes

For example, cat key.pub | pbcopy puts a file's contents on the clipboard, ready to paste into a web page. Running caffeinate -i before a long transfer stops the Mac from going to sleep halfway through.

Homebrew: the package manager

macOS doesn't include a package manager for command-line tools. The de facto standard is Homebrew, an independent project. You install it with the command shown on its official website and, once it's there, it works like this:

brew install wget
brew install --cask firefox
brew update
brew upgrade
brew list
brew uninstall wget

The difference between a formula and a cask is worth knowing: formulae are command-line programs and libraries, casks are graphical applications installed in the Applications folder. On Macs with Apple Silicon processors Homebrew installs into /opt/homebrew, on Intel ones into /usr/local; that is why some guides give different paths.

A practical tip: brew doctor flags the most common configuration problems, and brew cleanup removes old downloaded versions, which take up space over time.

launchd: the Mac's services

On the Mac, the role systemd plays on Linux belongs to launchd, the first process the system starts. It manages both system and user services, described in .plist files in XML format. The main folders are:

  • ~/Library/LaunchAgents: the user's services, started when they log in;
  • /Library/LaunchAgents and /Library/LaunchDaemons: services installed by third-party programs, for all users;
  • /System/Library/...: Apple's services, not to be touched.

The management command is launchctl. To see the user's services, use launchctl list. Many third-party programs install agents in these folders to start automatically or check for updates. When a Mac slows down for no apparent reason, in my work this is exactly where I look: leftover agents from uninstalled applications sometimes keep starting at every login. In recent versions of macOS, System Settings > General > Login Items also shows which apps have background items and lets you switch them off.

Activity Monitor and processes

Activity Monitor is the graphical equivalent of top. The most useful tabs are CPU, Memory and Energy. In the Memory tab, the Memory Pressure graph is what matters most: if it is green, the system has enough memory even if RAM looks almost full, because macOS compresses little-used pages before resorting to swap. From Terminal, top -o cpu sorts processes by processor usage.

Logs and diagnostics

The Console app shows system messages and application crash reports. From Terminal there is the log command:

log show --last 1h --predicate 'eventMessage contains "error"'
log stream --process Safari

The first shows the messages from the last hour containing «error», the second follows a process's messages live.

Disks and system protection

diskutil manages disks and volumes: diskutil list to see them, diskutil info disk2 for details, diskutil eraseDisk to initialise them, with the same caution as any destructive operation. The filesystem is APFS, with snapshots that Time Machine also uses.

Remember that the macOS system volume is read-only and protected by System Integrity Protection. It is normal for sudo not to let you change files under /System: customisations belong in /usr/local, /opt or your own user folder.

In short

  • Terminal uses zsh; commands are similar to Linux, but many options follow BSD.
  • open, pbcopy, mdfind and caffeinate are Mac-specific shortcuts.
  • Homebrew handles command-line programs (formulae) and applications (casks).
  • launchd and the .plist files in LaunchAgents control what starts automatically.
  • The system volume is read-only: changes go in user folders or /opt.
MM
Marco Marzaduri

Marco Marzaduri works in cybersecurity, digital forensics and web development. With degrees in computer science and psychology, he combines technical analysis with an understanding of the human factor.

Read also