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, pressqto quit. - Paths with spaces need quotes or backslashes:
cd "My Folder"orcd My\ Folder. Dragging from Finder adds the backslashes for you.
Moving around
| Command | What it does |
|---|---|
pwd | Print the folder you are in |
ls | List files in the current folder |
ls -la | Long listing including hidden (dot) files |
ls -lhS | Long listing with sizes like 4.2M, largest first |
ls -lt | Most recently modified first |
cd Documents | Go into a folder |
cd .. | Go up one level |
cd ~ or cd | Go 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
| Command | What it does |
|---|---|
mkdir projects | Create a folder |
mkdir -p a/b/c | Create nested folders in one go |
touch notes.txt | Create an empty file (or update its timestamp) |
cp report.pdf ~/Desktop/ | Copy a file |
cp -R photos photos-backup | Copy a folder and everything in it |
mv old.txt new.txt | Rename (or move) a file |
rm file.txt | Delete a file |
rm -r folder | Delete a folder and its contents |
cat notes.txt | Print a file |
less big.log | Page through a long file (q quits) |
head -n 20 file / tail -n 20 file | First or last 20 lines |
tail -f app.log | Follow 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
~/.zshrcwhen it starts. Aliases andPATHchanges 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=1to~/.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
- Apple Support: Open or quit Terminal on Mac
- Apple Support: Execute commands and run tools in Terminal on Mac
- Apple Support: Drag items into a Terminal window on Mac
- Apple Support: Keyboard shortcuts in Terminal on Mac
- Apple Support: Use zsh as the default shell on your Mac
- Apple Platform Support: Use network tools to test connections
- Apple open source: caffeinate(8) man page source
- Apple open source: ls(1) man page source (file_cmds)
- pbcopy(1) macOS man page (mirror)
- open(1) macOS man page (mirror)