ach5

Mac Terminal Commands: The Ultimate Guide 2021

ach5

In This ArticleWhat You Can Do With TerminalGetting StartedBasic CommandsPopular Commands

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 and What Do You Do With It?

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. 

Getty Images 

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.

Getting Started With Terminal

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. 

Basic Terminal Commands

If you want to manipulate files and folders easily, here are some simple commands to use:

Keyboard Shortcuts to Use With Terminal

TabAuto-completes files and folder namesCtrl + AMoves to the beginning of the line you are typing onCtrl + EMoves to the end of the line you are typing on Ctrl + WDeletes the word before the cursorCtrl + TSwaps the last two characters before the cursorEsc + TSwaps the last two words before the cursorCtrl + LClears the screenCtrl + CKills the current processCtrl + RSearch through previously used commandsOption + →Move the cursor one word forwardOption + ←Move the cursor one word backwardCtrl + FMove the cursor one character forwardCtrl + BMove the cursor one character backwardCtrl + YPaste whatever was cut by the last commandCtrl + _Undo the last commandCtrl + DExit the current shell

Work With Files and Folders

cd Shows your home directorycd <folder> Change to a specific directorycd ~ Home directory, e.g. ‘cd ~/folder/'cd /Root of drivels Shows a listing of all files in the current directoryls -CSort files or entries by sizels -ltList the files sorted by time modified (most recent first)ls -l Shows a long listing of all files in the current directory.ls -aListing including hidden filesls -lhLong listing with human readable file sizes ( KB, MB, or GB)ls -R Shows the entire contents of folder recursivelytopDisplays the active processes. Press q to quit q ExitclearClear the screentouch <file>Create a new file with no extensionpwdFull path to the working directory..Parent directoryls -l ..Long listing of parent directorycd ../../Move 2 levels up.Current folder catThe current folderrm <file>Remove a filerm -i <file>Remove with confirmationrm -r <dir>Remove a directory and its contents -Use with caution!rm -f <file>Force a removal without confirmationrm -i <file>Will display prompt before removalcp <file> <newfile>Copy a file to filecp <file> <dir>Copy a file to a directorymv <file> <new filename>Move/Rename a filemkdir <dir>Create new directory named <dir>mkdir <dir> <dir2> <dir3>Create multiple directories at oncemkdir -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 usernano <file>Opens the Terminal editoropen <file>Opens a file<command> -hShow help about a commandman <command>Show the help manual of the command

Control Permissions

ls -ldDisplay the default permission for a home directoryls -ld/<dir>Display the read, write, and access permission of a particular folderchmod 755 <file>Change the permission of a file to 755chmod -R 600 <dir>Change the permission of a folder (and its contents) to 600chown <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 statuswhois <domain>Output WHOIS information about a domaincurl -O <url/to/file>Download a file via HTTP, HTTPS, or FTPssh <username>@<host>Establish an SSH connection to <host> with user <username>scp <file><user>@<host>:/remote/pathCopy 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 com.apple.finder AppleShowAllFiles TRUEkillall 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 com.apple.screencapture 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 com.apple.screencapture 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.