Apple's macOS is a POSIX-compliant UNIX operating system designed to run on Mac computers. The system features access to the command-line interface through the native Terminal app or third-party terminal emulators.
While many Mac terminal commands are similar to Linux commands, macOS also features system-specific syntax designed to help Mac users manage their hardware and software.
This article provides a comprehensive list of Mac terminal commands alongside a downloadable PDF cheat sheet for easy reference.
Note: Since both macOS and Linux are compatible with UNIX applications, most commands listed in this overview have their Linux equivalents. While the commands produce the same results in both systems, certain functionalities may differ. Follow the links throughout the article to read Linux tutorials for each command.
Basic commands for the Mac terminal include utilities for obtaining administrative privileges, manipulating the command output, and finding help. Read more about those commands in the sections below.
Use the sudo command to authenticate as an administrator:
sudo [command] [arguments]
Example
Remove a directory the current user does not own:
sudo rm -rf testdir
There are two common scenarios for forwarding command output in macOS:
[command1] | [command2]
Example
Use the wc command to count the number of words in the output:
echo "one two three" | wc -w
[command] > /dev/null
Print text to standard output with the echo command:
echo [text]
Example
Print the value of the HOME variable:
echo $HOME
The system keeps a record of the executed commands. See the list of the commands you previously used by typing:
history
Helpful ways in which you can use the command history include:
history -[number-of-items]
Example
See the last five entries:
history -5
![value]
Example
Execute the entry number 1009:
!1009
Mac terminal comes with built-in help features. You can access command-line help in two ways:
[command] --help
Example
Read the help summary for the sudo command:
sudo --help
man [command]
Exit the current terminal session:
exit
Use the commands listed in the following sections to manipulate the files on your system. The commands cover all the basic file management operations, such as creating, modifying, and organizing the files.
Create an empty file:
touch [filename]
Example
Create a file named test.txt :
touch test.txt
Open any file on the system:
open [filename]
If the terminal cannot open the file, macOS detects the default application and opens the file in a separate window.
Example
Open an image on your system:
open pnap.png
Below is the list of the commands and options for viewing files in macOS:
cat [filename]
Example
Output the contents of the /etc/paths file:
cat /etc/paths
less [filename]
head [filename]
head -n [number] [filename]
Example
Print the first three lines of the /etc/paths file:
head -n 3 /etc/paths
macOS comes with two preinstalled command-line editors, Nano and Vim. Open the file in a command-line text editor by typing:
[text-editor-command] [filename]
Example
Open the file named test.txt in Nano:
nano test.txt
Use the >> symbols to append content to a text file:
echo "[text]" >> [path-to-file]/[filename]
Example
Add the words "This is a test file" to the file test.txt :
echo "This is a test file" >> test.txt
You can also append the contents of an entire file to another file:
cat [filename] >> [path-to-destination-file]/[filename]
Overwrite the contents of a file from the command line by using the > symbol.
echo "[text]" > [path-to-file]/[filename]
If the file does not exist, the system creates it.
By using cat , you can replace the entire file contents with another file:
cat [filename] > [path-to-file]/[filename]
Use the cp command to copy files in the Mac terminal. The basic syntax for copying a file is:
cp [path-to-file] [destination-path]
Example
Copy test.txt to the testdir directory located in the home directory:
cp test.txt ~/testdir
Other capabilities of the cp command include:
cp [filename] [new-filename]
cp [filename] [destination-path]/[new-filename]
cp "[spaced filename]" [destination-path]
cp [file1] [file2] [file3] [destination-path]
cp -i [filename] [destination-path]
The mv command moves the file to another location. The basic mv syntax is:
mv [path-to-file] [destination-path]
Example
Move the test.txt file to the home directory:
mv test.txt ~
Use mv to perform several more advanced actions, such as:
mv [filename] [new-filename]
mv [filename] [destination-path]/[new-filename]
mv -i [filename] [destination-path]
mv *.[extension] [destination-path]
Example
Move all the YAML files to the testdir directory:
mv *.yaml ~/testdir
Use the rm command to delete files from the system:
rm [filename]
rm -i [filename]
rm -f [filename]
rm [file1] [file2] [file3]
The sections below present the most common commands for working with directories in a macOS terminal.
Display the name of the current working directory with the pwd command:
The cd command helps the directory tree navigation in macOS. The following is the basic syntax for the command:
cd [directory-path]
If the user does not specify the path, cd opens the home directory. Alternatively, to go to the home directory, type:
You can also use the command to gain quick access to:
View the contents of a directory with the ls command:
Useful ls options include:
ls -a
ls -l
ls -S
ls -C
Note: Combine the options for more views. For example, to see all the files in a detailed list, type:
ls -la
Use the cp command with the -r option to copy directories:
cp -r [directory] [destination-path]
Example
Copy the testdir directory to the home directory:
cp -r testdir ~
To copy directory contents rather than the directory itself, use the ditto command:
ditto [directory] [destination-path]
The mv command moves the directory to another directory. The syntax is the same as with moving files:
mv [directory] [destination-path]
Delete directories with the rm command and the -r option:
rm -r [directory]
Add the -f option to force-remove all the files and sub-directories:
rm -rf [directory]
Mac terminal allows users to access information about file and directory sizes and the available storage space. The sections below list the commands related to storage monitoring.
The du command outputs the amount of space utilized by files and subdirectories in the current directory:
Useful du options include:
du -s [file1] [file2]
du -h
du -k
Example
Display memory in megabytes and pipe that output to the sort command to display the directories and files in descending order according to their size:
du -m | sort -nr
Display the free disk space of the system.
df -h
The -h flag shows the values using the powers of 1024. To change the values to the powers of 1000, use the -H flag:
df -H
Managing permissions in the Mac terminal includes viewing and changing access privileges related to specific files and directories and changing the item ownership. The following sections explain permission management in more detail.
View the permissions related to a specific file by using the -l option and providing the filename as the argument:
ls -l [filename]
Example: View the permissions of test.txt :
ls -l test.txt
To view directory permissions, add the -d option:
ls -ld [directory-path]
If no directory path is specified, the command displays the permissions for the current working directory.
Change read, write, and execute privileges of a file with the chmod command. The syntax for changing file permissions is:
chmod [number] [filename]
The [number] is a three-digit number in which digits represent user, group, and owner permissions. The number is a sum of all the permissions (read, write, and execute) given to the user, group, and owner.
The numerical values of the permissions are:
Example
Change the permissions of the test.txt so that only the user can read, write, and execute it:
chmod 700 test.txt
Use the -R option to change directory permissions:
chmod -R [number] [directory]
Change which user owns the file by using the following syntax:
chown [username]:[group] [filename]
To change directory ownership, add the -R flag.
chown -R [username]:[group] [directory-path]
Monitoring processes helps the user get a better picture of resource consumption on the machine and troubleshoot potential issues. Read this section to learn how to list, find, and stop running processes on a Mac machine.
List the currently running processes sorted by PID (Process ID) with the ps command:
ps -ax
To see more details about each process, including the CPU and memory consumption, enter the following command:
ps aux
For a detailed process list that updates in real time, use the top command:
By default, top refreshes the view every second. Set a custom refresh interval by typing:
top -s [number-of-seconds]
Example
Refresh top data every 10 seconds:
top -s 10
Adjust the view in top to see the data sorted by memory usage:
top -o rsize
To sort the processes by CPU, type:
top -o cpu
Search for a specific process by piping the output of the ps command to grep:
ps -ax | grep [process-name-or-PID]
Use the kill command to quit a misbehaving process by entering its PID:
kill [PID]
Quit a process by its name with the killall command:
killall [process-name]
Mac terminal supports many networking options, such as viewing and configuring the local network, connecting to remote computers, etc. The sections below explain how to perform the most common network operations.
Test if a remote host is reachable on the network with the ping command:
ping [hostname-or-IP-address]
Use the arp command to view a list of devices on the local network, with their IP and MAC addresses:
arp -a
View the path of the packets from the machine to the destination with the traceroute command:
traceroute [hostname-or-IP-address]
Display the connected network adapters with the ifconfig command.
ifconfig
To view a specific adapter, pass its name as an argument to the command:
ifconfig [network-adapter-name]
The curl command allows data transfers to and from remote servers. Use curl to download a file to your machine by using the -O option and passing the full URL as an argument:
curl -O [URL]/[filename]
Use the following syntax to establish an SSH connection with a remote server:
ssh [username]@[hostname-or-IP-address]
When connecting to a remote host for the first time, you may need to generate an SSH key:
ssh-keygen
After generating the key, copy it to the remote host:
ssh-copy-id -i [path-to-PUB-key] [username]@[hostname-or-IP-address]
Note: You can also use the PuTTY SSH client to establish a connection. Read How to Install PuTTY on Mac for a detailed tutorial.
Managing application execution parameters with environment variables is an important command-line functionality. Learn how to view and create variables in the following sections.
Display the list of all the variables on the system with the printenv command:
printenv
Use echo to print the value of a specific variable:
echo $[variable]
Add a new binary path to the PATH variable:
export PATH=$PATH:[path-to-executable]
Create a new variable by passing its name and value to the export command:
export [variable-name]=[variable-value]
Note: Variables created in this way will exist only for the duration of the session. To learn how to create permanent variables and learn more about environment variable management in macOS, read How to Set Environment Variables in macOS.
The following are the Mac terminal commands for finding files and content within files:
find [directory] -name "[filename]"
Example
Search for test.txt in the testing directory:
find testing -name "test.txt"
Example
Look for all the TXT files in the testing directory:
find testing -name "*.txt"
grep "[text]" [filename]
[command] | grep "[text]"
grep -rl "[text]" [directory]
Homebrew is the default package manager for macOS. It provides Mac with functionalities usually found in Linux package managers, such as APT, RPM, etc. Below is the list of the most common Homebrew operations.
Below are the operations you can perform on Homebrew formulae and casks:
brew update
brew upgrade
brew upgrade [formula]
brew pin [installed-formula]
brew unpin [pinned-formula]
brew install [formula]
brew uninstall [formula]
Learn more about the state of your homebrew formulae and casks by using the commands below:
brew list --formula
brew list --cask
brew deps [formula]
brew outdated --formula
brew outdated --cask
Troubleshoot potential problems with Homebrew with the following commands:
brew doctor
brew help
brew cleanup
The following is a list of useful terminal commands for maintaining your macOS system:
caffeinate
softwareupdate -l
sudo softwareupdate -i -a -R
killall Dock
defaults write com.apple.dock ResetLaunchPad -bool true; killall Dock
sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder
cd ~/Library/Mobile\ Documents/com~apple~CloudDocs/
sudo shutdown -r now
sudo shutdown -h now
Mac terminal has many useful keyboard shortcuts for managing terminal windows and navigating the terminal. Below is the list of the most helpful shortcuts, divided into categories for easier browsing.
Shortcut | Description |
---|---|
Command - N | Open a new window. |
Shift - Command - W | Close the window. |
Command - T | Open a new tab. |
Command - W | Close the tab. |
Option - Shift - Command - W | Close all terminal instances. |
Command - + / - | Make the text bigger/smaller. |
Command - D | Split the window into two panes. |
Shift - Command - D | Close the split pane. |
Shortcut | Description |
---|---|
Control - A | Move the insertion point to the beginning. |
Control - E | Move the insertion point to the end. |
Control - U | Delete the line. |
Control - K | Delete text from the active position to the end of the line. |
Option - Right Arrow | Move forward word by word. |
Option - Left Arrow | Move backward word by word. |
Shortcut | Description |
---|---|
Shift - Command - click the file path. | Select the entire file path. |
Triple-click the line. | Select the entire line. |
Command - X | Cut the selection. |
Command - C | Copy the selection. |
Option - Shift - Command - C | Copy plain text. |
Command - V | Paste. |
Command - F | Find text. |
Command - E | Find the preselected text. |
Command - J | Jump to the selected text. |
Command - A | Select all. |
Shortcut | Description |
---|---|
Control - Command - F | Full-screen mode on/off. |
Command - double-click the URL. | Open a URL. |
Command - P | Print. |
Command - K | Clear screen, except for the current prompt. |
Control - Shift - Command - ? | Open the man pages. |
We created a handy Mac terminal commands cheat sheet as a one-page reference for all the essential macOS commands. Save the PDF list of macOS commands by clicking the Download the macOS Cheat Sheet button below.
This Mac terminal guide listed all the important commands for navigating the terminal and performing the basic file, directory, and network management functions.
The guide also provided a PDF cheat sheet with the same commands on one easy-to-navigate page for quicker reference.