#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.
Related Premium Courses
#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 alternative are CLIs, on macOS also known as the 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:
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.
Let's get started with some core commands:
pwd = "print working directory" ls = "list items"
pwdprints our current path more about paths later in this article
lsshows all folders and files in this current directory
That's our current state, time to navigate:
cd = "change directory" cd / cd or cd ~ cd /Users/
cdgives us access to other directories
cd /navigates into our root directory
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 ../../)brings us one (two) hierarchy level(s) up, starting from our current folder
cd FolderName/OtherFolderNamewill 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
This project folder might contain subfolders, for example named "landing-page" and "checkout-page", so as absolute paths
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
./checkout. In many cases we can omit
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.typewill create a new (empty) file with the name and type specified
mkdir FolderNamecreates 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.
Deleting files or folders works as follows:
rm FileName.typeremoves the file referred to in the current folder (adding the path will delete the file in the respective folder)
rmdir FolderNameremoves the empty(!) folder in our current working directory
Important!: There is no way to bring back deleted files and folders. So be careful when applying these commands!
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 FolderNameremoves the folder including all files and folders it contains (in this case, the folder is also located in our current working directory)
-r is a "flag", allowing us to apply specific options to our comment. More on these can be found in the video on top of the page.
#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.htmlfile 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.htmlfile to the new-project folder
mv /project/index.html /project/index2.htmlwill rename the
That's it, with these basics you're now ready to explore the terminal on your own - have fun :)