A brief guide to the Mac terminal
The Command Line interface (also known as CLI for short) is accessible via the Terminal:
/Applications/Utilities/Terminal.
- A shell is the first command that executes automatically when you open the Terminal, to provide you with the actual CLI. Many types of shell are available in Mac OSX but the default is Bash (since Catalina it is ZSh)
What is a shell?
According to the source of all knowledge (Wikipedia) a shell is the following:
“In computing, a shell is a computer program which exposes an operating system‘s services to a human user or other program. In general, operating system shells use either a command-line interface (CLI) or graphical user interface (GUI), depending on a computer’s role and particular operation. It is named a shell because it is the outermost layer around the operating system.”
When you open the Terminal the command prompt will always show you the name of who you are logged on as, @ the name of the machine, followed by where you are in the CLI.
Last login: Wed Jan 27 09:48:07 on ttys001
boris@redfish ~ %
So, I am logged in as boris, to the device redfish and my location is:~
In Unix, symbol ~ is shorthand for the currently logged in users home folder. Paths are written just as you would write them in the Gui (for example in Go To Folder)
So, you could write the path to your documents folder like this:
~/Documents
Running a command
In the Graphical User Interface (GUI) you point and click on things. In the terminal, you type commands. Commands are essentially apps, but without a GUI. (In some cases, commands are actually part of Apps that also have a GUI! )
Your location on the command line
As mentioned, by default, when you open the terminal you will start in your home folder.
Let’s see what else is in the folder with:
ls the list command
Type:
ls
This returns the following information:
Desktop
Documents
Downloads
Google Drive
Library
Movies
Music
Pictures
Public
Shared
Basic commands
pwd print working directory – this shows current directory on screen
ls list directory – shows contents of wd
cd change directory – navigation command
Tab completion – press first letter and then use Tab – if more than one option Terminal will flash. You can then add other letters and it will try and fill in the word like predictive text on a mobile
Recursive commands – e.g. that populate downwards so not only affect a file but affect all the contents of a folder
ls –R Public/ lists all contents of the Public folder, and the contents of the contents of the same folder
e.g:
ls –R /Users/boris/Public/Drop\ Box
local webserver issue fix.rtfd
supersecret.conf.txt
cd .. change directory back – takes back to the previous folder~ indicates the current users home folder
Instead of selecting different options via a menu or a keyboard shortcut everything is typed so after the main command, you may add options that will change the command’s behaviour. These are called switches.
Elevation to Root
If you login as an admin and type:
sudo -s
you will elevate as the root user.
Any commands you then run – for the next 15 mins – will be executed with root privilege. After that you will need to authenticate again but this can go on as long as you need.
Deleting Stuff
To delete stuff you use the rm (remove) command, plus an relevant switches:
(HEALTH WARNING! – in the terminal (like in space) noone can hear you scream!
Files will normally be deleted without any prompting so always CHECK your syntax as mistakes can be costly!)
For example:
If you are logged in as user: bgates and you want to delete the file “macsAreReallyBetter.docx” from your desktop type this:
rm /Users/bgates/Desktop/macsAreReallyBetter.docx
If you want to delete a folder you need to use the -R switch (for recursive)
rm -R /Users/bgates/Desktop/photosOfMyMac
Until you are really comfortable in the terminal it is better to always provide the full path to the document you want to delete, to be sure that you are affecting the correct files. When you’re more confident there are path abbreviations which can be useful.
Controlling the Date and Time
If you need to manually set the date and time on a device this can be done via the Terminal.
As an example, the following command
sudo date 020715312023
Sets the date to: 07 Feb 2023 1531
This need to be run as an admin user.
That means that if you are not logged in to the device as an admin user (in this case ladmin) you need to switch user to the admin user.
You don’t need to actually logout and log back in again just run the su (switchuser) command in Terminal to switch to ladmin
e.g: su ladmin
then run:
sudo date 020715312023
(the second part should be adjusted for the actual current time that you want to set – see below)
Breaking the command down:
date this is the actual command
02 Month number 02 – February
07 Day 07
1531 Time in 24 clock – e.g. 15:31 (3:51pm)
2023 Year – e.g. 2023!
Using that logic you can set any date and time you want.