简繁EN
← ToolsTools · Guide

A macOS Terminal Command Cheat Sheet

Files and folders, system info, networking, permissions, text, processes and macOS-only commands, plus pipes, redirection and a few handy combinations.

Date
Reading3 min read
Tags#macOS#Terminal

Commonly used commands for the macOS Terminal app, grouped by purpose. The default shell on macOS is zsh; every command below ships with the system and was checked on macOS 26. Replace anything in angle brackets <> with your own file name, path and so on, and leave out the brackets.

Files and folders

Command What it does
pwd Show the current directory path
cd <directory> Change to a directory
cd ~ Go to your home directory
cd .. Go up one level
cd - Go back to the previous directory

Listing and searching

Command What it does
ls List files and directories
ls -la List all files in detail, including hidden ones
find <path> -name <file name> Find files
mdfind <keyword> Search with Spotlight

Working with files

Command What it does
cat <file> Print a file’s contents
less <file> View a file page by page; press q to quit
head -n 10 <file> Show the first 10 lines
tail -n 10 <file> Show the last 10 lines
cp <source> <destination> Copy a file
mv <source> <destination> Move or rename a file
rm <file> Delete a file
rm -rf <directory> Delete a directory and everything in it (use with care)

Files deleted with rm don’t go to the Trash and can’t be recovered.

Directories

Command What it does
mkdir <name> Create a directory
rmdir <name> Delete an empty directory
open . Open the current directory in Finder
open <file> Open a file with its default app

System information

Command What it does
top Show processes and resource usage; press q to quit
ps aux Show all running processes
df -h Show disk space usage
du -sh <directory> Show the size of a directory
uname -a Show system information
sw_vers Show the macOS version

Networking

Command What it does
ping <domain or IP> Test a network connection; press Control + C to stop
curl <URL> Download a file or fetch a web page
ifconfig Show network interfaces
netstat -an Show network connections
nslookup <domain> Look up DNS records

Permissions and users

Command What it does
sudo <command> Run a command as administrator; asks for your login password
chmod <mode> <file> Change file permissions
chown <user> <file> Change a file’s owner
whoami Show the current user name

Text

Command What it does
grep <keyword> <file> Search for text
wc -l <file> Count lines
diff <file1> <file2> Compare two files

Processes

Command or key What it does
kill <process ID> Stop a process
killall <process name> Stop all processes with that name
bg Resume a suspended process in the background
fg Bring a background process to the foreground
Control + C Stop the running command
Control + Z Suspend the current process

macOS-only commands

Command What it does
pbcopy / pbpaste Copy to and paste from the clipboard, e.g. cat file.txt | pbcopy
say <text> Speak text aloud
screencapture <file name> Take a screenshot
caffeinate Keep the Mac awake; press Control + C to end
defaults read / defaults write Read or change system and app preferences

Handy tricks

Command or key What it does
Tab Autocomplete commands and paths
↑ ↓ Browse command history
Control + R Search command history
!! Run the previous command again
history Show command history
clear Clear the screen
man <command> Read a command’s manual; press q to quit

Pipes and redirection

Syntax What it does
command1 | command2 Send the output of command 1 to command 2
command > file Write output to a file, replacing its contents
command >> file Append output to the end of a file
command < file Read input from a file

Handy combinations

# Find and delete every .log file under the current directory
find . -name "*.log" -delete

# Count lines of code in all .py files
find . -name "*.py" | xargs wc -l

# Show the 10 largest files or folders in the current directory
du -sh * | sort -hr | head -10

# Watch new lines in the system log; press Control+C to stop
tail -f /var/log/system.log

# Rename every .txt file in the current directory to .md
for f in *.txt; do mv "$f" "${f%.txt}.md"; done

Run man <command> to see the full usage of any command.