Software & OS

Mac Terminal Commands: The Ones Worth Knowing (With Examples)

The Mac Terminal commands you will use most are cd, ls, pwd, mkdir, cp, mv and rm for moving around and handling files, plus macOS-only tools like open, pbcopy and caffeinate. Open Terminal from Spotlight (Command-Space, type "Terminal") or from /Applications/Utilities, type a command and press Return.

Two habits save a lot of typing: drag a file or folder from Finder into the Terminal window and its full path appears on the command line, and press the Up Arrow to bring back earlier commands.

Before you start: the shell, the prompt and stopping things

  • Your shell is zsh. Since macOS Catalina (10.15), zsh is the default login and interactive shell for new accounts. Check with echo $SHELL.
  • Stop a running command with Control-C. Apple's guide says it "sends a signal that causes most commands to terminate".
  • Typos produce zsh: command not found: .... Check spelling first.
  • Read the manual for any command with man ls. Scroll with the arrow keys or Space, press q to quit.
  • Paths with spaces need quotes or backslashes: cd "My Folder" or cd My\ Folder. Dragging from Finder adds the backslashes for you.

Moving around

CommandWhat it does
pwdPrint the folder you are in
lsList files in the current folder
ls -laLong listing including hidden (dot) files
ls -lhSLong listing with sizes like 4.2M, largest first
ls -ltMost recently modified first
cd DocumentsGo into a folder
cd ..Go up one level
cd ~ or cdGo to your home folder
cd -Go back to the previous folder

The ls on a Mac is the BSD version. Its man page documents -a (include entries starting with a dot), -l (long format), -h (unit suffixes with -l), -S (sort by size, largest first), -t (sort by modification time, newest first) and -G (colour output). If you are used to Linux, ls --color may not behave as you expect; use -G.

Working with files and folders

CommandWhat it does
mkdir projectsCreate a folder
mkdir -p a/b/cCreate nested folders in one go
touch notes.txtCreate an empty file (or update its timestamp)
cp report.pdf ~/Desktop/Copy a file
cp -R photos photos-backupCopy a folder and everything in it
mv old.txt new.txtRename (or move) a file
rm file.txtDelete a file
rm -r folderDelete a folder and its contents
cat notes.txtPrint a file
less big.logPage through a long file (q quits)
head -n 20 file / tail -n 20 fileFirst or last 20 lines
tail -f app.logFollow a log as it grows (Control-C stops)

rm does not move anything to the Trash. Deleted files are gone, so check the path first, and add -i (rm -i) if you want a confirmation for each file.

Finding things

find ~/Documents -name "*.pdf"
grep -rn "TODO" ~/projects/my-app

find searches by name, size, date and more; grep searches inside files. Both work on a Mac much as they do on Linux. See find command examples and grep command examples for the details.

macOS-only Terminal commands

open: open files, folders, apps and URLs

open .                       # this folder in Finder
open report.pdf              # with the default app
open -a TextEdit notes.txt   # with a specific app
open -R report.pdf           # reveal it in Finder
open https://example.com     # in the default browser

The man page describes open as opening a file, folder or URL "just as if you had double-clicked the file's icon". -a picks the application, -e opens with TextEdit, -t with the default text editor, and -R reveals the file in Finder instead of opening it.

pbcopy and pbpaste: the clipboard

pbcopy < ~/.ssh/id_ed25519.pub   # copy a file's contents
ls -la | pbcopy                   # copy a command's output
pbpaste > clipboard.txt           # save the clipboard to a file

pbcopy "takes the standard input and places it in the specified pasteboard", which by default is the normal clipboard you paste from with Command-V. pbpaste writes the clipboard to standard output. The first example is the usual way to copy an SSH public key into a website's settings page; generating an SSH key covers creating one.

caffeinate: keep the Mac awake

caffeinate                 # stay awake until Control-C
caffeinate -d              # also keep the display on
caffeinate -t 3600         # stay awake for 3,600 seconds (1 hour)
caffeinate -i make         # stay awake while a command runs

From Apple's man page: with no flags, caffeinate prevents idle sleep until it exits. -d keeps the display awake, -i prevents idle sleep, -s prevents system sleep (only on AC power), and -t sets a timeout in seconds. If you give it a command, the assertion lasts as long as that command runs, which is handy for long downloads, backups or builds.

networkQuality, ping and nc: test the network

networkQuality
ping -c 4 example.com
nc -v -z example.com 443

Apple's platform support guide lists these. networkQuality measures upload capacity, download capacity and responsiveness against Apple's servers and rates responsiveness as Low, Medium or High. ping checks whether another device answers; on macOS it keeps going until you press Control-C unless you limit it with -c 4. nc -v -z host port checks a single port: "succeeded" means it is open, "Connection refused" means the host answered but nothing is listening, and a timeout points to a firewall or routing problem. For reading ping results, see the ping command.

Running things as administrator

sudo command

sudo runs one command with administrator rights and asks for your login password. Nothing appears on screen while you type the password; that is normal. Only use it when a command fails with Permission denied and you understand why it needs more access. A mistyped sudo rm -r can delete files the system needs.

Changing your shell and its settings

  • Switch the default shell: chsh -s /bin/zsh (or /bin/bash). The valid choices are listed in /etc/shells. You can also change it in System Settings under Users & Groups, Advanced Options, "Login shell".
  • zsh reads ~/.zshrc when it starts. Aliases and PATH changes go there.
  • If you still use bash and want to hide the message saying zsh is now the default, Apple's documented fix is adding export BASH_SILENCE_DEPRECATION_WARNING=1 to ~/.bash_profile.

Keyboard tricks in the Terminal

  • Up Arrow / Down Arrow: step through previous commands.
  • Tab: complete a file or folder name; press it twice to see the options.
  • Control-C: stop the current command.
  • Control-A / Control-E: jump to the start or end of the line.
  • Control-R: search your command history; type part of an old command.
  • Command-K: clear the window back to the start.

All of these are listed in Apple's Terminal keyboard shortcuts reference. If you want to go further with the standard Unix commands, basic Linux commands applies almost entirely to the Mac.

Frequently asked questions

How do I open Terminal on a Mac?
Press Command-Space to open Spotlight, type Terminal and press Return. Or open Finder, go to Applications, then Utilities, and double-click Terminal.
Which shell does the Mac Terminal use?
zsh, since macOS Catalina (10.15). Older accounts that were created with bash may still use it. Run echo $SHELL to see yours, and chsh -s /bin/zsh to switch.
How do I stop a command that is running in Terminal?
Press Control-C. It sends an interrupt signal that makes most commands stop. If a program has taken over the whole window, such as man or less, press q to quit it instead.
Are Mac Terminal commands the same as Linux commands?
Mostly. Both have ls, cd, cp, mv, grep, find and the other standard Unix tools, so most commands carry over. Options sometimes differ because macOS ships BSD versions of many tools while Linux usually ships GNU versions, and macOS adds its own tools such as open, pbcopy and caffeinate.

Sources