Extras

Basic Command Line Instructions on Mac

When we turn on our computers, we usually tell them what to do using a graphical user interface. But, you can also give a computer instructions by typing into a command line interface.

  • Graphical user interfaces (GUIs) let users tell a computer what to do using visual representations and symbols. For example, the user can point a mouse at an icon and click on it.
  • Command line interfaces (CLIs) let users tell a computer what to do by typing instructions (or commands).

Developers often need to run tools that do not have GUIs; therefore, they need to know how to use the command line.

Macs have a utility called Terminal which provides a command line interface. You can find it in the /Applications/Utilities folder.

Terminal implements something called a shell, which is both a language and an interpreter that is used to run the instructions written in that language.

Terminal has used a shell called Z shell (zsh) since macOS Catalina. Before that, it used another shell called Bash.

Once you have typed a command into Terminal, you press the enter or return key to run it, and a response will be shown in text.

Starting Terminal

To start Terminal, go to the /Applications/Utilities folder and select Terminal. This will open a small window with some text in it.

The text it opens with is called the prompt. (It may look slightly different on your computer.) It often shows you the five pieces of information that are outlined below.

The term command line refers to the area to the right of the prompt. This is where you will type commands that should be performed, and then see the response that they generate.

The terminal window displaying the prompt ( the text that makes up the prompt is described next).
A diagram of the prompt which reads userName@computerName directory % []. Parts are described in the table below.
KEY
User name Name used to log into your computer
Computer name Name of the computer
Working directory Folder the command prompt is currently in
Prompt symbol Separates information above from the commands you type and the response the commands return. For Zsh, it is a $ symbol.
For Bash, it is a $ symbol.
Cursor A box to show the position where text will appear

NOTE: When working with Finder, you can see which folder you are in.

When working with the command line, the working directory shows you which folder you are in.

You need to know the working directory because many of the commands you use will be applied to the files in that folder.

It is possible to customize or edit the information shown in the prompt.


Structure of Commands

The instructions you type into the terminal consist of up to three parts:

  • Commands are words that tell the interpreter what you want it to do.
  • Options start with a dash symbol (-) and they turn an option on.
  • Arguments provide extra information the command needs.

Once you have entered a command (and any options or arguments that are needed), you press the enter or return key to run the command.

A diagram showing the three parts of a sample command. The parts are descibed next.

Using Basic Commands

Open Terminal and enter the command: pwd
Then press the enter or return key.

pwd stands for print working directory.
It will show the absolute path to the working directory.
If you just started Terminal, this will usually be your user folder:

/Users/YourUserName
NOTE: When you start Terminal, the current working directory is often shown as a tilde symbol ~. This symbol is used to represent to the current user's directory.

Terminal showing the command pwd, and the response /Users/username

Next, enter the command: ls
Then press the enter or return key.

ls stands for list.
It will list all of the directories and files in the working directory.

Terminal showing the command ls, and a list of folders in the user's directory

Next, enter the command: clear
Then press the enter or return key.

clear will clear the Terminal screen and just show the prompt. This is very helpful because the screen can easily get cluttered.

Terminal showing the prompt because the clear command was used. This adds a scroll bar and the line shown is at the top.

Using Commands with Options

Enter the ls command again and add the -a option after it:

ls -a

Then press the enter or return key.

The -a option states that all files and folders should be shown (including hidden and system files).

This is the equivalent of pressing command + . in Finder to show hidden files and folders.

Any entries that start with a period are hidden or system files.

Terminal showing all files and folders in user's directory. The hidden files start with a . symbol.

Enter the ls command again, then the -a option, followed by the -F option:

ls -a -F

Then press the enter or return key.

The -F option tells Terminal to add a forward slash / after anything that is a folder (making it easier to distinguish between files and folders).

Terminal showing all files and folders in user's directory with forward slash after any folder names.

Using a Command and an Argument

Enter the ls command again.
Then the -F option.
Then add the argument /Applications after the option.

ls -F /Applications

Then press the enter or return key.

The argument for the ls command is the path to the folder whose contents should be listed. Each command can have different arguments.

In this case, the command will show all of the files and folders in the /Applications folder.

Terminal showing a list of all files and folders in the /Applications directory. Folders end with forward slash.

Moving Around the File System

When using a GUI like Finder, you can click on folder names to navigate around the directories and files on your computer. When using Terminal, you can use the following commands to navigate the file system:

Command Description
cd Changes the directory
ls Lists the files and folders in the working directory
pwd Prints the working directory (this is used to show where you are)

You often need to navigate to the correct directory before running a command in Terminal to ensure that the command is run on the files or folders that it contains.


Changing Directories

Enter the cd command, followed by the path to a folder you want Terminal to move to. For example:

cd /Library

Then press the enter or return key.

This will make the folder specified in the path (in this case, it is the /Library folder), the working directory.

The path specified in the argument can be a relative path or an absolute path. Note how the working directory in the screen shot has been updated from a tilde symbol in the prompt so you can see where you are.

Terminal window showing that the working directory has changed to /Library.

Below you can see more examples of arguments that could be used with the cd command:

Argument Description
cd / Root folder (equivalent of Macintosh HD)
cd ~ User’s home directory (equivalent of /Users/UserName)
cd .. Go up one directory
cd /FolderName If a path starts with a / it is an absolute path from the file system root directory
cd FolderName/FolderName If the path does not start with a / it is relative to the current working directory

NOTE: If you want to specify a folder or filename that contains a space, you should add quote marks around that segment of the file path. The quotes can be single or double quotes. E.g.: cd /Applications/"Adobe XD"


Create, Copy, Move and Delete Files

When using a GUI, like Finder, you can use keyboard shortcuts such as cmd + c to copy, cmd + v to paste, and you can drag files between folders to move them.

To create files or folders, and copy, move, and past them using Terminal, you need to use the following commands. Each one uses the path(s) to the file or folder as an argument.

To see the following examples working:

  • In Finder, go to your user home directory
  • In Terminal, go to your user home directory by entering the command: cd ~
Command Description
touch Change a timestamp on a file (indicating when it was last opened) - if the named file does not exist, it will create a blank file with this name
mkdir Create directory
cp Copy
mv Move
rm Remove file
rmdir Remove directory

Creating a Directory

Make sure you are in your user folder.
Enter the mkdir command.
Add the argument test.

mkdir test

Then press the enter or return key. If you look in Finder when this command is run, you will see a folder called test appear.

TRY: Use the same command to create a folder called test2.
(This will be needed for the next example.)

Terminal window showing mkdir command and Finder window showing the new directory.

Creating a File

Enter the touch command. Then the argument test/test.txt.

touch test/text.txt

Then press the enter or return key. If you look in Finder when this command is run, you will see that a blank text file called test.txt was created in the test folder.

Terminal window showing touch command used to create new file and Finder window showing the new file created.

Updating Modified Time of File

Click on the file that was just created in Finder.
Look at the time it was created and modified.
Enter the same command again at the command line:

touch test/test.txt

Then press the enter or return key.
Then look at the last modified time in Finder and you should see that it will have been updated to the time that this command was run.

Terminal window showing touch command to update last modified time, and Finder window showing modified time being later than created time.

Copying a File

Enter the cp command.
Then the path to the file to copy.
Then the path to where the file should be copied.

cp test/text.txt test2/text.txt

Then press the enter or return key.
If you created the test2 folder earlier, this should copy test/text.txt to test2/text.txt.

If you did not, you will see a message saying No such file or directory.

Terminal window showing cp command to move file and Finder window showing the file copied to new folder.

Deleting a File

Enter the rm command (remove).
Followed by the path to the file to remove:

rm test2/text.txt

Then press the enter or return key.
This will delete the test2/text.txt file.

NOTE: This command will delete ANY specified file.
It will not ask you to confirm before deleting it.

Terminal window showing rm command to remove file and Finder window showing empty folder.

Moving a File

Enter the mv command (move).
Then the path to the file to move.
Then the path to where it should be moved:

mv test/text.txt test2/text.txt

Then press the enter or return key.
This will move the file test/text.txt to test2/text.txt.

Terminal window showing mv command to move file, and Finder window showing the moved file.

Finding a File

Enter the find command.
Then the path to the folder you want to search inside.
The -name option to find any filenames that contain the search term.
Then the term you are looking for.

find /Applications -name "*Terminal*"

Then press the enter or return key. You should see the paths to any files in the specified folder whose names contain the search term.

To search the contents of files, remove the -name option.

The search term can use regular expressions (as described in our PHP & MySQL book). Here, the use of the * character means that there can be any number of other characters before or after the search term in the filename.

Unlike the Spotlight search tool on a Mac, the find command searches hidden and system files, and their contents.

Terminal window showing find command and Finder window showing list of paths to files containing that name.

Download a File from the Internet

Enter the curl command. Then the -o option.
Then the URL of a file you want to download.

curl -o test.png https://eg.link/img/test.png

Then press the enter or return key.

This will get the file from the URL and save it to your Downloads folder.

The -o option is needed to save the data that is sent back as a file.

Terminal window showing curl command to download file, and details of the file that was downloaded.

Viewing Options for Commands

Enter man command.
Then the name of a command whose options you want to view.

man find

Then press the enter or return key.

It will show you a manual of options and arguments for that command in the Terminal window.

You will need to press q to go back to the command prompt.

Terminal window showing details of the find command, after the man command has been used to display them.