Academind Logo

The macOS Terminal (zsh) - Getting Started

How does the macOS terminal work, why should I use it and what's the difference between a Graphical User Interface (GUI) and a Command Line Interface (CLI)? These and more questions will be answered in this video & article!

Created by Manuel Lorenz
#

Computer Interaction: End User vs. Programmer

Before diving into the world of programming, the typical user - computer interaction happens via a GUI, a Graphical User Interface.

As a programmer our view changes from end user to creator and so does our way to interact with the computer. We move from graphical interfaces to the command line.

This article (and the video on top) will give you a thorough introduction into macOS' Command Line Interface (CLI), explain what it is, why we use it and dive into its basic commands to get you started.

#

GUI vs CLI

GUIs are interfaces created to make the user experience as convenient as possible. The finder on macOS is such a GUI:

The macOS finder

The alternative are CLIs, on macOS also known as the terminal:

The macOS terminal

Both are so-called shells. A shell manages the communication between end user and computer. Sounds strange? Opening folders with a double-click only works as the finder (our shell - a GUI) manages the communication to "tell" the computer that double-clicking a folder in the shell means "opening it".

With a CLI, a text input by the user would be translated to do the same.

Both shells are important. Whilst a GUI typically is required for a smooth end user experience, CLIs are important for programmers to perform specific tasks, e.g. execute code or run certain file types (Python files as an example).

#

macOS: Terminal & Z-Shell (zsh)

On macOS, the terminal is our "hardware" for the shell, so the tool where our shell software can run. In the past, the bash-shell was the standard, nowadays being replaced by the z-shell (zsh) which we'll also use in this tutorial.

#

Getting Started with Root, Users & Home Directories

CMD + Shift brings up the spotlight search on the Mac, a quick search for terminal starts it.

By default the terminal navigates us to the home folder. We have three core folders (or directories) on our Mac: root, users and this home folder.

A quick switch to our finder and specifically to "locations" where we select our device brings light into these:

Select your device in locations on the Mac

Data stored on the highest hierarchy level on your hard disk drive are in the root directory (containing folders like Applications and System), data stored in the users folder are in the users directory and the highest hierarchy level in a specific user's folder (here LorenzM - my name) is the home directory.

With that out of the way, the command line fun can begin.

#

Basic Navigation

Let's get started with some core commands:

pwd = "print working directory"
ls = "list items"

That's our current state, time to navigate:

cd = "change directory"
cd /
cd or cd ~
cd /Users/
  • cd gives us access to other directories

  • cd / navigates into our root directory

  • cd or cd~ will bring us to the home directory

For the users directory we don't have a shortcut, the absolute path will help here though:

  • cd /Users/ brings us to the users directory

Let's continue with direct folder access beyond our root, users and home directories:

cd ..
cd FolderName/OtherFolderName or cd ./FolderName/OtherFolderName
  • cd .. or (cd ../../) brings us one (two) hierarchy level(s) up, starting from our current folder

  • cd FolderName/OtherFolderName will navigate into the corresponding folder

#

Absolute vs Relative Paths

We have absolute and relative paths:

Absolute paths always start with the root folder, so with / - A typical path could be /Users/YourUserName/project.

This project folder might contain subfolders, for example named "landing-page" and "checkout-page", so as absolute paths /Users/YourUserName/project/landing-page and /Users/YourUserName/project/checkout.

Relative paths start from the folder you're currently working in.

./ sets the current folder as starting point, so from our project folder, navigating into the subfolders would simply mean ./landing-page and ./checkout. In many cases we can omit ./ though.

The two examples also show an important advantage of relative paths:

If the full path changes (for example the user name), the project would break as our initial path is no longer existing. With relative paths we stay within our project folder, changes in higher hierarchy levels don't impact our project.

#

Creating & Deleting Files and Folders

Besides navigation, file and folder creation is also possible in the terminal:

  • touch FileName.type will create a new (empty) file with the name and type specified

  • mkdir FolderName creates a new folder with the name specified

Both files and folders can also be created in specific directories when adding the corresponding path, e.g. touch project/FileName.type.

Deleting files or folders works as follows:

  • rm FileName.type removes the file referred to in the current folder (adding the path will delete the file in the respective folder)

  • rmdir FolderName removes the empty(!) folder in our current working directory

Removing directories which contain files and/or folders is possible, but must be done with caution as you can cause severe damage to your system when removing wrong files or folders.

  • rm -r FolderName removes the folder including all files and folders it contains (in this case, the folder is also located in our current working directory)

#

Copying and Moving Files and Folders

Copying and moving files and folders requires a source path (which file/folder should I copy/move?) and a target path (where should the data be copied/moved to?):

  • cp /project/index.html /new-project/ will copy the index.html file from the "project" folder into the "new-project" folder

  • cp -r /project/ /new-project/ will copy the project folder including all files and folders it may contain into the new-project folder

  • mv /project/index.html /new-project/ will move the index.html file to the new-project folder

  • mv /project/index.html /project/index2.html will rename the index.html file

That's it, with these basics you're now ready to explore the terminal on your own - have fun :)