The Mac Terminal

The bluffers guide

Image for post
Image for post
The terminal

The Terminal App allows you to control your Mac using the command line. So the Mac terminal is used for many things, including Git (unless you are using the potentially dreadful Xcode interface).

You can’t use the mouse in Terminal, so make sure that you have your keyboard at the ready! This could well be due to the history of the command line (from the 1960s), yet the command line retains a huge amount of power.

Prerequisites:

  • Use of a Mac
  • Understanding of relative, absolute paths and your home directory

Terminology

Bash: “Bourne again shell”. There are many shells that can run UNIX commands, and the one used on the Mac Terminal is Bash.

Command line: An interface for interacting with the operating system.

Shell: A user interface that allows you to operate the services on an operating system. Bash is the default shell for terminal.

Shell bulltin: A command or function executed in the shell. Similar to executing an external program, but fasters.

Terminal: A command line interface to control the UNIX-based operating system that underlies the Mac.

Vim: A text editor.

Wildcards: * (any character) and ? (any single character).

This may depend on your Mac configuration. My Favourite way is to use Spotlight through a Command⌘-Space key combination and type “Terminal”. If you have a favourite way, by all means use that.

Writing tricky characters

Tilde is used when traversing (moving through) the file system on a Mac. On a UK keyboard, access this on the left-hand side of your keyboard and press the left shift and the ~button. ~ is an alias for the home directory.

Stopping getting stuck

It is possible to get stuck on a menu. The solution can be to quit

q Quit

To interrupt a command that is already running,

Control-C — Interrupt a running command

You should know that commands are always run from the current location. This is particularly important when traversing the file system (so if you don’t specify a location the default will be where you last moved to).

It is also possible to get stuck in VIM, which is a text editor with it’s own commands. Be careful here with the use of the colon (:) as it is required for these commands to work!

:q — to quit

:w — write your file (i.e.: save)

:wq — write and quit (save and exit!)

The commands

Each command is made up of three elements — The command, an argument and an option.

There is an inbuilt manual that is accesibile from anywhere! Simply type man

$ man

(note the above $ symbol represents the terminal in this guide)

Did you notice that the manual is rubbish? Use whatis in front of your target command (providing on whether you know what the command is) before you run it, so here if we want to find out some information about cd:

$ whatis cd

To view all commands available in Terminal at a specific time, hold down the Escape key and then press y.

Traversing the file system

You should be

ls (list)

$ ls

ls This tells you what is in the current folder (ls = list). Other switches are possible, for example ls -a lists all files including hidden files.

ls the home directory

This is referred to with tilde (~) as the home directory (ie. /home/user where user is usually your name!):

$ ls ~

ls with wildcards

You can list using wildcards!

List files that start with A:

$ ls A*

Note you can restrict the list to directories only and not the folder contents you can use -d:

$ ls -d A*

note: you can place * anywhere in the filename, and the filter still works. You can even use file extensions. If you need to find specific files you can use a brace expression

$ ls *.{txt,md}

pwd (Print working Directory)

$ pwd

cd (change directory) to the parent directory

$ cd..

Go to the documents folder

$ cd ~/documents

Go to the previous directory (this is cached for you!)

$ cd -

Go to your home directory

$ cd ~

or

$ cd

Go to the documents directory, pushing to the stack

$ cd pushd / documents

then at any time later we can return to the documents directory, popping from the stack

$ cd popd

mkdir (make directory)

$ mkdir “DirectoryName”

touch README.md (make a readme file for a github repo)

$ touch README.md

mv allows you to move a file from one location to another location

$ mv readme.md ~/documents

(providing you are in the correct folder with readme.md will move the file to documents).

Just move the file from one name to another!

$ mv readme.md readmetwo.md

Removing is always quite worrying. This means that we should really use a -i argument to make sure that you are sure that the file should be removed, if you are feeling brave remove the -i argument(!)

$ rm — i readmetwo.md

if you want to remove a folder you can use the -r switch (the following deletes the .git fonder created from a git init command).

$ rm -rf .git

Tips and tricks

You can clear a terminal window with the command clear (easy!)

$ clear

Use the up and down cursor keys to browse through previous commands. You can then press return to run that command.

Terminal has a form of autocomplete when you press the tab key.

You can drag and drop a file into the terminal window, and the path is displayed in the terminal window (this is a nice shortcut!).

You can add an ampersand (&) to the end of a long-running command and it will run in the background. This allows you to type further commands while the long-running command runs in the background.

Sometimes you need to run a command as an administrator (or you don’t have sufficient rights to perform the command you wish to) you can add sudo to the beginning of your command (as a prefix). Unfortunately this gives a command the ability to move or delete system critical files, so it is necessary to know what a command does before you run it with sudo!

To create the banner above, experiment with the Banner command.

You can open the finder from the current location with a simple

$ open .

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store