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.
1. When I say ‘write’ and ‘type’ in the examples, press enter to execute the commands.
2. The $ symbolsbelow are 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
Making and Removing:
Copying, Renaming and Moving:
Opening, Viewing and Streaming:
5 – Exiting the Command Line:
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.
$ pwd /Users/Jem $ ls Applications Desktop Downloads Documents Movies Pictures $ cd Documents/ $ pwd /Users/Jem/Documents $ cd $ pwd /Users/Jem
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 .. $ pwd /Users/Jem/Documents/version-control/game/new-game $ cd ../../.. $ pwd /Users/Jem/Documents
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 $ ls oldgame $ cd old-game $ touch index.html script.js $ rm index.html script.js $ cd .. $ pwd /Users/Jem/Documents/version-control/game/old-game $ 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/ $ ls art-info.txt To-finish $ cp art-info.txt poster.txt $ ls art-info.txt poster.txt To-finish $ mv art-info.txt To-finish/art-info-to-do.txt $ ls poster.txt To-finish $ cd To-finish $ ls art-info-to-do.txt
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!