How to Use Mac Terminal Commands

Your cheat sheet to boosting Mac productivity

If you are looking to get the most power out of your Mac, you need to embrace the Mac's terminal and learn some helpful terminal commands. Most users are fine using menus and the mouse to control settings, files, and folders, but if you want to get down under the hood of macOS, terminal can get you there quickly and get the job done. 

What Is Mac Terminal?

Mac terminal is an included program on macOS that allows you to use Unix commands (think DOS) to perform dozens of functions quickly and easily without any interference from the operating system. The technical term for it is a command-line interface (CLI). Using the terminal can be very powerful and efficient, but also dangerous. 

Be very careful when using terminal commands. If misused, you could end up deleting files and folders faster than you can react (to the point where you might need to restore it from a backup.) 

Terminal is sometimes compared to Windows command prompt program. They are similar; however terminal uses Unix commands to function. Windows uses its own command prompt language. 

Some users (think IT administrators and developers) use the terminal because it can be faster than the graphical interface most of us use and, the real kicker, it allows access to functions not available through the regular macOS interface. 

When using terminal commands, precision matters (including capitalization), so be careful of every character, even spaces.

How to Open Terminal (Command Prompt) on Mac

You can use terminal by opening Finder > Applications > Utilities > Terminal. A window will open, and you will see a command prompt ending with a $. The command line shown is ComputerName:CurrentDirectory ~Username$. At the top of the window, you will see the word Bash; it stands for 'Bourne again shell.' Bash is the language used for terminal on a Mac.

You are ready to start typing commands.

After every command, press the Return key to execute it. You can also copy and paste into the terminal window. 

Keyboard Shortcuts to Use With Terminal

Tab Auto-completes files and folder names
Ctrl + A Moves to the beginning of the line you are typing on
Ctrl + E Moves to the end of the line you are typing on 
Ctrl + W Deletes the word before the cursor
Ctrl + T Swaps the last two characters before the cursor
Esc + T Swaps the last two words before the cursor
Ctrl + L Clears the screen
Ctrl + C Kills the current process
Ctrl + R Search through previously used commands
Option + → Move the cursor one word forward
Option + ← Move the cursor one word backward
Ctrl + F Move the cursor one character forward
Ctrl + B Move the cursor one character backward
Ctrl + Y Paste whatever was cut by the last command
Ctrl + _ Undo the last command
Ctrl + D Exit the current shell

Working With Files and Folders

cd  Shows your home directory
cd <folder>  Change to a specific directory
cd ~  Home directory, e.g. ‘cd ~/folder/’
cd / Root of drive
ls  Shows a listing of all files in the current directory
ls -C Sort files or entries by size
ls -lt List the files sorted by time modified (most recent first)
ls -l  Shows a long listing of all files in the current directory.
ls -a Listing including hidden files
ls -lh Long listing with human readable file sizes ( KB, MB, or GB)
ls -R  Shows the entire contents of folder recursively
top Displays the active processes. Press q to quit 
clear Clear the screen
touch <file> Create a new file with no extension
pwd Full path to the working directory
.. Parent directory
ls -l .. Long listing of parent directory
cd ../../ Move 2 levels up
. Current folder 
cat The current folder
rm <file> Remove a file
rm -i <file> Remove with confirmation
rm -r <dir> Remove a directory and its contents - Use with caution!
rm -f <file> Force a removal without confirmation
rm -i <file> Will display prompt before removal
cp <file> <newfile> Copy a file to file
cp <file> <dir> Copy a file to a directory
mv <file> <new filename> Move/Rename a file
mkdir <dir> Create new directory named <dir>
mkdir <dir> <dir2> <dir3> Create multiple directories at once
mkdir -p <dir>/<dir> Create nested directories 
rmdir <dir> Remove an entire directory ( only works on empty directories )
find <dir> -name <"file"> Find all files named <file> inside <dir>. Use wildcards (*) to search for partial filenames

Special Commands

sudo <command> Run a command with the security privileges of super user
nano <file> Opens the Terminal editor
open <file> Opens a file
<command> -h Show help about a command
man <command> Show the help manual of the command

Control Permissions

ls -ld Display the default permission for a home directory
ls -ld/<dir> Display the read, write, and access permission of a particular folder
chmod 755 <file> Change the permission of a file to 755
chmod -R 600 <dir> Change the permission of a folder (and its contents) to 600
chown <user>:<group> <file> Change the ownership of a file to user and group. Add -R to include folder contents

Network and Server Commands

ping <host> Ping a host and display its status
whois <domain> Output WHOIS information about a domain
curl -O <url/to/file> Download a file via HTTP, HTTPS, or FTP
ssh <username>@<host> Establish an SSH connection to <host> with user <username>
scp <file><user>@<host>:/remote/path Copy a <file> to a remote <host>

The Most Popular Mac Terminal Commands

As a bonus to our terminal cheat sheet, here are some of the most popular terminal commands to get the most out of your Mac.

Show/Hide Hidden Files and Folders

There are times when you need to view hidden files and folders on a Mac, and there is a command for that.

defaults write AppleShowAllFiles TRUE
killall Finder

To reverse the command above, change TRUE to FALSE.

Download Files From the Internet

To quickly download a file from the internet use the command below.

curl -O URL of the file you want to download

Change the Default Location for Screen Shots

Not everyone wants their screenshots saved to the Desktop. With a single command, you can change where they go.

defaults write location path to folder where you want screenshots to be saved

Press Return.

killall SystemUIServer

Press Return.

Change the Default File Type for Screenshots

If you want to change the default file type of screenshots, use the following command.

defaults write type JPG

Press Return.

killall SystemUIServer

Press Return.

Delete All Files in a Folder

You can easily delete all files in a folder using a single command. However, be warned there is no undo button, once you press Return, the files are gone.

rm -R foldername

You must include the entire path for the folder name.

Read the Manual

Bash has a complete manual built-in, and you can access the pages through terminal using the “man” command.

For example, if you wanted to know how to use the cd command, you would type in: 

man cd

You can scroll through pages of the manual by pressing the spacebar. 

These are the most common commands, but there are thousands (you can even combine them into one series of commands) you can try in terminal to make your Mac experience more powerful and more enjoyable. But remember, there's a lot of power in these commands so make sure you type them in correctly.

Was this page helpful?