Getting Started with the Command Line on MacOS
We all spy or eavesdrop a little on our neighbour in cafés… or at least catch what they’re doing in our peripheral vision. Don’t pretend!
This time, about a month ago, I remember noticing my neighbour typing strange commands in a strange black window. Graphical interface nada. Don’t get me wrong, I love words (I did English lit at uni) but I’m also very much a visual animal; I live and breath art and design. So the text interface was intimating. But I was also mesmerised and wondered what she was doing and how it all worked… Cue another cup of tea (how British), a lot of online tutorials, and tackling the command line step by step.
Just a quick side note that learning the command line was part of the precourse material for coding bootcamp Founders and Coders (which I’m starting in late October) and the content below is based on the recommended material provided.
I remember sitting beside someone in a café writing super fast in Terminal. What was the
cd she kept writing? And why was it making me feel so nervous? I loved watching programmers do this in movies, marvelled at it, so what was the problem? Reading other blogs I learnt that it’s normal to be scared initially. So I breathed, ordered another cup of tea (how British) and took to learning it step by step.
1. When I say ‘write’ and ‘type’ in the examples, press enter to execute the commands.
2. Don’t write the $ symbol before the command shown in the examples. It’s just used to ‘denote what follows is a command to be run’ (Treehouse).
3. Be careful: there’s no undo button so only write commands you’re familiar with.
1 — Definitions
2 — Opening the Command Line
3 — Navigating Your Computer
Fast-forward vs Take a Step Back:
4 — Files and Directories
Making and Removing:
Copying, Renaming and Moving:
Opening, Viewing and Streaming:
5 — Exiting the Command Line:
Time Saving Techniques
I started with learning a few definitions. Here are a few:
The command line is your computer’s text interface. 1) Type command and press enter to execute 2) System responds 3) Repeat steps 1 and 2.
MacOS’s Terminal application “allows you to access the complete UNIX environment using standard commands, tools, and scripting languages” (Web.Archive).
The prompt includes information about you and your computer, ending with a $ symbol e.g
Jems-Macbook:~ jem$. You write commands after the $.
A directory is a technical word for ‘folder’.
2. Opening the Command Line
To open Terminal you can either search ‘Terminal’ in Spotlight or find it in your Utilities folder (Applications > Utilities > Terminal).
3. Navigating Your Computer
The first commands to learn are:
pwd Print working directory. It's is a 'Where am I?' command e.g. my Home directory
cd <directory> Change directory. To change to the Documents directory from Home write
cd Documents. To go back Home type
NB. If you want to make sure the directory exists add a / at the end:
ls List all files and folders in a directory.
I check my working directory (
pwd), list all the folders inside it (
ls), change directory to Documents (
cd Documents/) and then go back Home (
cd). This process is equivalent to going to the Home folder in Finder, having a look at the folders, double clicking on Documents and then going back Home.
$ cd Documents/
Fast-forward vs Take a Step Back
You can fast forward to a file or folder using paths. If I wanted to navigate to my game folder (Documents > version-control > game) without writing multiple commands, for example, I could write:
I’m now in game but what if I want to go back to version-control? Type
cd .. and you're done.
You can also use paths to go back multiple directories. Start with
cd .. and add
/.. (one directory) to go another step back. So to go back three folders, for example, you'd write
I navigate to my nested part-1 (
cd Documents/version-control/game/new-game/part-1), go back one folder to new-game (
cd ..) and then back three to Documents (
$ cd Documents/version-control/game/new-game/part-1
$ cd ..
$ cd ../../..
4. Files and Directories
Making and Removing
mkdir <directory> Make a directory ie. create a folder called Coding by writing
touch <file> Make a file. Add an index.html in your Coding folder by typing
touch index.html. You can easily add multiple files too by separating each name with a space:
touch index.html styles.css.
rmdir <directory> Remove a directory. Go to the directory that contains Coding e.g. Documents and then write
rmdir Coding. If this doesn't work (after making sure the folder is empty) use
rm -rf i.e
rm -rf Coding.
rm <filename> Remove a file ie. delete a file called styles.css by writing
NB. Again, be careful with the remove command.
I go to my game folder (
cd Documents/version-control/game) and create an old-game folder (
I then step into into my new directory (
cd old-game) and create an HTML and JS file (
touch index.html script.js).
Hmm I’ve changed my mind… so I remove them (
rm index.html script.js) and delete the old-game empty folder (
cd .. and
rmdir old-game). All fixed.
$ cd Documents/version-control/game/
$ mkdir old-game
$ cd old-game
$ touch index.html script.js
$ rm index.html script.js
$ cd ..
$ rmdir old-game
Copying, Renaming and Moving
cp Copy a file or directory.
Write the file you want to copy followed by the copy’s name:
cp index.html index2.html.
You can do the same with directories by adding a
-r flag before the two names:
cp -r calculator calculator-to-fix.
mv Rename or move a file.
Rename a file by typing the target file and its replacement name ie.
mv copy.html index.html. Copy.html will now be called index.html.
Move a file by writing the target file and its new location ie.
mv copy.html calculator-to-fix/.
Move and rename by specifying both the new name and location ie. move copy.html to calculator-to-fix and rename it new-copy.html:
mv copy.html calculator-to-fix/new-copy.html.
I go to my Poster directory (
cd Documents/Poster/), copy art-info.txt and call the copy poster.txt (
cp art-info.txt poster.txt).
I decide to rename art-info.txt (as it still needs work) whilst also moving it to my To-finish directory (
mv art-info.txt To-finish/art-info-to-do.txt).
$ cd Documents/Poster/
$ cp art-info.txt poster.txt
$ mv art-info.txt To-finish/art-info-to-do.txt
$ cd To-finish
I’m still trying to understand
popd so will update the post with an example of this soon.
Opening, Viewing and Streaming
open <file> Open a file. To open your world-domination.txt navigate to your directory and type
open world-domination.txt. Tip: To open all files in a folder type
cat <file> The cat command will stream the contents of your file.
$ cat posters.txt
Need to brainstorm ideas for three posters by 22 Sep.
Posters will be distributed on 03 Oct.
less <file> View a file. You can look through its contents, search words by typing
/hello , and then exit by pressing q.
5. Exiting the Command Line
exit Type exit and you're out! As easy as that.
Time Saving Techniques
Tab to autocomplete: e.g. If you want to go to Pictures from Home (~/Pictures) type
cd Pi, press tab and the line will autocomplete to
cd Pictures/. Nifty!
Cycle through commands already executed with up and down arrow keys. If you frequently use the same command or are copying a path, this will really speed up your workflow.
Command Line Interface Crash Course | Recommended Material
For a more technical tutorial: Getting to Know the Command Line
I’ll be back soon with more content but until then you can my read my last post: 4 Ways I Got Into My Dream Coding Bootcamp and How You Can Too. See you in the next one!