Summary: This post is a beginner's guide to Mac Terminal. Everything you need to know is covered, including how to open and use Terminal on Mac and a list of some commonly-used commands.
We are all familiar with the GUI that we use daily to interact with our Macs, but not so much with the Mac command-line interface - Terminal. It's actually quite useful for people who are used to command lines.
It has the advantages of being more efficient with tasks, troubleshooting issues, and repairing disks through SSH or Mac Single User Mode when you lost access to the user account.
Let's start with some basics to help you dig deeper and work with complicated tasks in the future.
What is Terminal on Mac？
Generally speaking, people use two interfaces to communicate with computers: GUI (graphic user interface) and CLI (command-line interface). The Terminal app provides a command-line interface on Mac computers to allow you to control the operating system quickly and efficiently to perform tasks a graphical user interface is unable or requires more effort to deliver.
How to open Terminal on Mac?
If your Mac works well, you can launch Terminal from Desktop through Spotlight or Finder. Otherwise, you can access it from the macOS Recovery mode.
How to open Terminal from Desktop?
The easiest way to open Terminal is from Spotlight search. Click on the small magnifying glass icon at the top-right corner of your Mac or press Command + Space keys at the same time to launch Spotlight. Then you can type in "Terminal" and double-click the appeared Terminal. app to open it.
You can also find it in the Utilities folder under Applications in Finder.
If you plan to use Terminal often, you can pin it on the Dock by right-clicking on the Terminal icon and choose "Keep in Dock" from the Options menu. If you prefer to open Terminal on Mac with a keyboard only, you need to create a shortcut.
How to open Terminal from macOS Recovery mode?
To launch Terminal on an Intel-based Mac, you need to boot your Mac into macOS Recovery mode by holding down Command + R keys when you hear the chime at starting.
For M1 Macs, you need to shut down the computer first and press the power button until you see "Loading startup options", then click Option > Continue. Upon the macOS recovery window, you can pull down the Utilities menu and click on Terminal to open the Terminal app.
How to use Terminal on Mac?
Once you open Terminal, you will see a small window with the title listing the name of the current directory, the type of the shell, and the window's size in pixels. A command consists of three parts: the command name, a modifier to the command's output, and where the command will operate.
- Commands won't execute until you press the return key. If you mistake the code, you may receive an error message, or the command will run in the wrong way.
- You can use the up arrow key to repeat a command. If you want to clear a window occupied with texts, simply type in clear and press Return.
- You usually need to go to Finder to see the changes you made with Terminal, except the command requires a new window display.
Basic Mac Terminal Commands
We will explain some common commands you can use to finish simple tasks in this section. If your file system is case-sensitive, Terminal can only recognize the original name, so Desktop can't be desktop. When you are unsure which directory you are in, enter pwd to make a check.
How to change directory in Terminal?Command:
cd ~/desired directory
cd desired directory
By default, you are brought to the home directory when you open a Terminal window. If you need to navigate to another folder, you can copy this command to Terminal and change "desired directory" for the one you want to open. E.g. switching to Desktop, insert cd desktop. Don't forget to press Return after inputting the command.
How to list a directory in Terminal?Command:
After using the cd command to switch to the desired directory, you can utilize ls to view all the files or folders inside it.
How to create, open and delete a directory in Terminal?Create a new folder:
Create a new file:
mkdir folder name
For instance, I want to create a folder named test1. I will then put in mkdir test1 and press Enter. Now inside this folder, I can enter touch sample.txt to generate a txt file called sample.Open a file under current folder:
Open a file in another folder:
Open an application:
open file path
Open a file with a specified app:
Open -a Application name
Open -a app name file path
For example, to open the Music app, you can enter Open -a music. If you need to open a file with a specified app, let's say open a file called sample.txt with WPS Office, you can use Open -a wpsoffice /Users/jenny/test1/sample.txt. The file path can be easily dragged and dropped to the Terminal window.Delete a file:
Delete a folder:
rm -r folder name
Files deleted by this code are gone forever, and there is no way to get them back. So be cautious!Copy files from one folder to another:
ditto folder 1 folder 2
Folder 1contains files you want to copy, and folder 2 is where you want to paste the files.
How to find IP address on Mac Terminal?Wired internet connection:
ipconfig getifaddr en1
ipconfig getifaddr en0
Enter either one of the commands to find out your local IP address easily. Don't confuse ipconfig with the ifconfig used on Windows.
How to reset mac password with Terminal?Command:
Boot into Recovery mode, and type resetpassword after launching Terminal. You can then choose the account that you want to reset the password and enter the new password.
How to repair disks using Terminal?Verify the main disk:
Repair the main disk:
diskutil verifyvolume /
diskutil repairvolume /
To verify an external hard drive, for example, one called backup. You can enter diskutil verifyvolume /Volumes/backup/ and press Return. If it reports any error, you can use diskutil repairvolume /Volumes/backup/ to repair the external hard drive on Mac.
How to quit Mac Terminal?
You can quit a shell session first to avoid interrupting any ongoing process by typing in exit and pressing the enter key. Then on the Dock, you can quit Terminal by right-clicking on it and choose Quit.
What is bash in Mac?
There are different types of shells people use to communicate with computers. Bash is one of them, as well as the more recent zsh (z-shell). Bash was the default shell in Macs before macOS 10.15 Catalina, and since then, it is replaced by zsh.
Mac Terminal showing % instead of $?
For Macs using the zsh shell, you will see the % sign in the command prompt; and the $ sign is for the dash shell. If you are running macOS 10.15 Catalina and later, zsh is the default shell. Otherwise, it's dash. It's a command prompt that indicates where typing starts.
How to kill a process in Terminal?
You can interrupt a running process in Mac Terminal by pressing Control-C.