Beginners guide to using the command line on a Mac

I’ve decided to blog a command line course that I’ve been reading through. So here it is. The very very basic absolute beginners guide to using terminal on a Mac.

This will be an ongoing post so I will post as the course goes along.

Understand this before continuing:

  • Terminal is a program built into the Mac. It is what is called a command line interpreter.
  • A command line interpreter is a program that enables you to ‘program’ the computer to do things. Automate tasks, create folders, change file names and much more.
  • These types of programs are called ‘shells’. Terminal is a particular type of shell called the ‘bash’ shell.
  • Modern web development and computer programming involves using the Terminal to set up tasks that automate processes on your local development environment. The Terminal is used to run commands and get programs to do things.
  • Lots of super things can be achieved using the Terminal and a lot of it is easy – you just have to learn how!

The first thing you need to know is how to navigate around your Mac in order to get to certain folders and files.

Chapter 1: Navigating Directories

Where is the Terminal on the Mac?
Go to Applications, it is in the Utilities folder:

/Applications/Utilities/Terminal.app

Open it.
You know you can open another tab (cmd T) as and when you want.

The first steps to getting used to using the Terminal is in navigating your directory structure.

Some very common commands you will be using a lot:

ls – List everything in a directory

You can provide ‘switches’ to a command that fine tunes your list.
ls -ltr
l = list
t = sort by time modifies
r = reverse results

This is what appears:
drwx------+ 39 Dan staff 1056 22 May 22:49 Desktop

Breakdown of what these characters mean:
d = directory, the following characters are what permissions are avaliable:
r = read. The directory can be read.
w = Write. The directory can be changed.
x = Excecute. Programs can be executed from the directory.
The characters after these letters signify if other people sharing my computer can do things.

How do I get to another folder (directory)?
cd = change directory

Why would I want to go to another folder?
Programs can be used to perform actions on files within particular folders.

Type cd Documents to go to the documents folder
Or,use autocomplete:

Press tab to autocomplete:

Start to type ‘Docu’ and press tab to autocomplete the command. So ‘cd Docu [tab].

Type just ‘d’. Autocomplete advises which of the 2 folders: Documents or Downloads?

So…Using ls and autocomplete means that the full name of the directory doesn’t have to be typed, every time. Just ‘ls’, see the directory, then type the first few letters of the directory and press tab. This is a much quicker and simpler form of navigation.

How to get to the home directory?
Cd ~
Use the tilde character ‘~’.

How to clear the terminal screen?
Type ‘clear
Or ctrl L.

How to navigate to a folder within a folder?
Just continue the path using a slash
cd folder/another-folder

How to find the path of the current located directory?
Type ‘pwd’.
Pwd = show path of Present Working Directory

Terminal shows the path of the directory:
/Users/Dan/Documents/folder/another-folder

How to navigate up the directory tree?
Type 2 full-stops (periods):
cd .. = go to the parent directory

This saves from having to start from the home directory again, just to move up one level. So from the example above:
/Users/Dan/Documents/folder/another-folder

Typing cd ..
Will move to
/Users/Dan/Documents/folder/

This can be typed multiple times to move up to other levels:

cd ../../ etc.

Note that the slash directory system is the same for must websites. This is because Macs use a ‘unix’ file system, the same as websites.

Next chapter:
Navigating shortcuts…