On macOS, the terminal is, fittingly, called Terminal, and it uses the Z shell (Zsh) as the default login shell. (Before macOS Catalina, Apple was using Bash shell as the default shell). Using this Terminal, you can easily navigate system directories, copy/move files, get system information, securely remote login to other systems, and automate tasks/actions on your Mac efficiently. However, to perform any of these actions, you need familiarity with terminal commands (and their syntaxes). In this guide, we’ll cover all the essential macOS Terminal commands you need to know and how to use them effectively on your Mac.

Essential macOS Terminal Commands

Executing commands on any operating system requires a terminal. macOS already comes equipped with one, and you can find it under Applications > Utilities. Alternatively, you can use the Spotlight search to look for Terminal. Additionally, you can also add it to your dock for quick access. For this, run Terminal, right-click on its icon in the dock, and select Options > Keep in Dock. Opening up the Terminal window brings up the Mac command prompt which looks like a black box. Now, all you need to do is type in a terminal command and hit return to execute it. For your convenience, we’ve classified command line commands into several categories so it’s easier to follow them:

1. Basic Terminal Commands

Before you jump into action-specific Terminal commands, below are some basic commands you should know.

i. man

The man command displays a user manual of the command for which you make the query. Using it, you can get more information about a command, such as its description, usage, available options, and variations, among other things. For example: man cd will give you all the details you need to know about the cd (change directory) command.

ii. clear

As its name suggests, the clear command clears the shell and gives you a blank window to input your commands. So if you’ve got your Terminal window filled with results from all your previous commands, simply run clear to get a clean slate.

iii. sudo

sudo is the most powerful terminal command. It gives you administrative (root) privileges to execute actions on macOS. You’ll need to use it when you want to execute a command that demands superuser access. For example, if you want to shut down your Mac through Terminal, you’ll need to run: sudo shutdown and enter your user password for the command to execute.

iv. history

The history command comes in handy when you want to find out all the commands you’ve executed in the past. For instance, if you’ve disabled/enabled some service on your Mac but don’t exactly remember its name or the command you’ve used, you can use this command to find out the service and revert your action.

2. Navigating Directories

Moving between different directories or folders is one of the basic actions you’ll have to perform to navigate your file system. You’ll need to perform it when you want to create a new file, move a file between directories, or launch programs within a directory. However, before you change directories or folders, it’s important to know your present working directory. To do this, run: pwd Similarly, you might also need to view the contents of a directory or a folder to identify if it holds the file/directory you want to access. Use the following command and its variations to do this effectively: ls For a detailed breakdown: ls -l To view all the contents of a directory, including the hidden files and directories: ls -al Once you’ve identified your current working directory and the directory you want to navigate to, the cd command will help you move between directories. The following is an explanation of how to use it. To go to the Home directory: cd or cd ~ When you want to move to a particular directory or folder: cd directory_name Eg: cd Downloads To move up to the parent directory: cd .. If you want to go back to the previous working directory or folder: cd - Lastly, when you have to go to the root directory, run: cd /

3. Directory Management

Once you navigate to your desired directory or folder, you can perform several operations there, everything from creating and editing new directories to and deleting the existing ones. To create a directory, run: mkdir directory_name When you want to create multiple directories or folders at once: mkdir directory_name_1 directory_name_2 directory_name_3 If you want to remove/delete a directory or folder, run: rmdir directory_name For times when you want to delete a non-empty directory, you can use the -R (recursive) option to delete the directory/folder along with all its content: rm -R directory_name

4. File Management

Much like directory management, Terminal also lets you perform file operations, so you can create new files, edit them, and delete the ones you don’t need. To create a file: touch file_name If you want to create and edit a file, run: nano file_name which will open the file in the Nano text editor. Alternatively, if you prefer using some other text editor, replace nano with the name of that editor in the above command. To view the file type of a file on your Mac: file file_name For times when you want to copy a file from your current directory to another directory/folder, run: cp file_name directory_name Eg: cp myfile ~/Desktop/MyDocs If there’s a need to copy a file to the same directory, but with a different name: cp file_name new_file_name Besides copying, sometimes there’s a need to move files between different directories. When such needs arise, you can move a file from your current directory to another directory with: mv file_name directory_name Eg: mv myfile ~/Documents/MyDocs Moreover, the mv command also doubles as a rename command. To use it to rename your file, use the following syntax: mv current_file_name new_file_name When you want to remove/delete a file, run: rm file_name

5. Installing Programs using Terminal commands

macOS comes pre-installed with the Homebrew package manager, which lets you install programs on your Mac using the Terminal. In some sense, it’s a much easier way to install apps on your computer, as opposed to the traditional way where you’d need to go through a series of steps. To update the Homebrew repository, run: brew update If you want to upgrade all the installed packages on your system: brew upgrade When there’s a GUI-based application that you need to install, we’ll have to use Cask: brew install –cask program_name Eg: brew install –cask vlc If it’s a non-GUI program: brew install program_name Do note that not all packages/programs can be installed right away: you might need to add their source repository to fetch them before running the install command. Finally, if you want to remove a program, use: brew uninstall program_name

6. Network Management

macOS allows you to view detailed information about your network configuration right from the Terminal window. Although you can use the GUI to view such information, the CLI approach makes the entire process quick and easy and saves you extra steps. One of the most basic network operations is to ping a website/IP address to check for connectivity. To do this, enter: ping hostname Eg: ping google.com or ping 142.250.192.14 If you want to view your device’s IP address and MAC address, run: ifconfig en0 To find the IP address and the MAC address of all the devices connected to your network: arp -a When you need information about the incoming and outgoing connections to your Mac, use: netstat For finding all the running processes on your Mac that have an active internet connection: lsof To get more information about a domain, use: whois domain_name Eg: whois google.com If you want to identify the path (and the hops) traversed by the packets from your device and to their destination address, run: traceroute hostname Eg: traceroute google.com

7. Process Management

If you’ve ever opened the Activity Monitor app on your Mac, you’d have seen all the active processes running on your system. These processes can be system apps, third-party apps, or other background services required by the operating system. While, in general, you wouldn’t encounter issues with these processes, sometimes when you have a lot of them running on your device — to a point where it’s borderline close to your maximum memory/CPU limit — you might experience some lag on your system. One way to deal with this is to use the Activity Monitor app. However, a much easier way to do it is to use the Terminal. The first step is to identify the running processes on your system. To do this, run: ps -ax Alternatively, if you’d like to know the status of the top processes that are currently running, you can do so with: top Press q or control + C to stop. Now, if you notice an unfamiliar process or a process that’s consuming a lot of your resources in the output of any of the above commands, you can get more information about it by running: ps -ax | grep program_name Eg: ps -ax | grep Safari Here, you can see the application name under the CMD column. It’s usually listed with the absolute path of the program/application. Upon getting to know more about the process, if you find the need to terminate it, run: sudo killall program_name Ex: sudo killall systemuiserver or sudo kill PID Since we’re running the kill/killall command with sudo, you’ll need to enter your user password after entering the command to execute it.

8. Permission Management

Permissions on an operating system define who can access and modify files/directories on a computer. If you’ve got multiple users sharing the same system, you can set permissions for each user to limit their access and prevent them from viewing or modifying your system (or other) files. Setting permissions on the Terminal is fairly easy once you get the hang of its syntax. However, before you move onto it, the first thing you’ll need to do is identify the file permissions for the file you want to modify. To do this, type: ls -al file_name You should be able to see file permissions on the left side of the output. A file/directory permission usually comprises eleven characters: the first character indicates if it’s a file or a directory, the next nine characters signify the permissions (and are split into groups of three, and the final character identifies if the file/directory carries extended attributes. The first character is always either hyphen (–) or letter (d): the former represents a file, whereas the latter signifies a directory. Moving to the next nine characters, these are divided into three groups: file/directory owner, group, and other user permissions. Each of these nine places is occupied with the following characters: – (no permission), r (read), w (write), or x (execute). By putting these characters together, you can set the permissions for a file/directory. Here’s how the permissions can be formed:

— represents no read, write, execute permissions. r– shows only read permission. rw- means that the file can only be read and written. rwx signifies that the file can be read, written, and executed. r-x means that the file can only be read and executed.

Alternatively, you can also use numeric notation, wherein the above characters are replaced with numbers. It constitutes a total of eight numbers, and here’s a breakdown of what they represent:

0 – no permissions 1 – execute 2 – write 3 – execute and write 4 – read 5 – read and execute 6 – read and write 7 – read, write, and execute

Lastly, the eleventh character in permission notation is @. It’s referred to as an extended attribute and is unique to specific files and directories. With the basics out, here’s how to incorporate the above information to set permissions. To set up read, write, and execute permissions for all three access classes, run: chmod ugo+rwx file_name In numeric representation, you’ll need to use: chmod 777 file_name To do the same for all text files in a directory: chmod ugo+rwx *txt When there’s a need to provide all user classes with the same permission: chmod a+rwx file_name If you want to set permission in a way that the user class gets all three access while the group gets read and write access and other users only get read access, you’ll need to use the following command: chmod ugo+rwxrw-r– file_name With the numeric representation: chmod 764 file_name To remove write and execute permissions for the group and other users classes, enter: chmod go-wx file_name or chmod 744 file_name If you’re finding it hard to use the numerical representation, you can use a chmod calculator to deduce the permission denotation for your permission requirements.

9. Ownership Management

While chmod gives you the ability to change the file/directory permissions to limit its access, it doesn’t let you dictate who owns a file/directory. This is where the chown command comes into the picture and helps you change the ownership of files/directories on your Mac. In case you’re unsure which command to use when, here’s a tip: if you want to change what users on your Mac can do with your files, you need chmod, whereas when you want to change who owns a file, you need chown. To change the ownership of a file, use the following syntax: chown user name file_name Eg: chown user1 myfile To know your user_name, run whoami in the Terminal. Alternatively, to find a list of all users on your Mac, enter: ls /users After this, if you want to alter the ownership of a file/directory that you don’t have access to, you can use sudo to force your changes: sudo chown user_name path/to/file Note that, you’ll also need to prepend the absolute path (ie. path from the root directory) for your file. For instance, if your file is in Documents, you’ll need to use the following syntax ~/Documents/Directory_Name/File_Name. Eg: sudo chown user1 ~/Documents/MyFolder/myfile

Use Your Mac Efficiently With Terminal Commands

We’ve barely scratched the surface with Terminal commands in this listicle: there’s a slew of other commands that you can use to perform pretty much all kinds of actions on your Mac. However, that said, the commands we’ve listed above will surely help you get hold of the Terminal and enable you to use it effectively to perform trivial actions on your Mac quickly and efficiently. And over time, as you become familiar with it, you’ll be able to use it more proficiently.

Essential macOS Terminal Commands You Need to Know  Guide  - 95Essential macOS Terminal Commands You Need to Know  Guide  - 41