command-line

Session on the command line, a means of interacting with your computer programmatically through text.

View the Project on GitHub dhsouthbend/command-line

«< Previous Next »>

Navigation

Prefatory pro tips

Go slow at first and check your spelling!

One of the biggest things you can do to make sure your code runs correctly and you can use the command line successfully is to make sure you check your spelling! Keep this in mind today, this week, and your whole life. If at first something doesn’t work, check your spelling! Unlike in human reading, where letters operate simultaneously as atomistic symbols and as complex contingencies (check Johanna Drucker on the alphabet), in coding, each character has a discrete function including (especially!) spaces.

Keep in mind that the command line and file systems on Mac OS and Unix are usually pre-configured as cAsE-pReSeRvInG–so capitalizations also matter when typing commands and file and folder names.

Also, while copying and pasting from this handy tutorial may be tempting to avoid spelling errors and other things, we encourage you not to! Typing out each command will help you remember them and how they work.

Getting started: know thyself

You may also see your username to the left of the command prompt $. Let’s try our first command. Type the following and press the enter key:

$ whoami

The whoami command should print out your username. Congrats, you’ve executed your first command! This is a basic pattern of use in the command line: type a command, press enter on your keyboard, and receive output.

Orienting yourself in the command line: folders

OK, we’re going to try another command. But first, let’s make sure we understand some things about how your computer’s filesystem works.

Your computer’s files are organized in what’s known as a hierarchical filesystem. That means there’s a top level or “root” folder on your system. That folder has other folders in it, and those folders have folders in them, and so on. You can draw these relationships in a tree:

Users
|
-- jojokarlin
  |
  -- Applications
  -- Desktop
  -- Documents

The root or highest-level folder on OSX is just called /. We won’t need to go in there, though, since that’s mostly just files for the operating system. On Windows, the root directory is usually called C: (More on why C is default on Windows).

Note that we are using the word “directory” interchangeably with “folder”–they both refer to the same thing.

OK, let’s try a command that tells us where we are in the filesystem:

$ pwd

You should get output like /Users/jojo. That means you’re in the jojo directory in the Users folder inside the / or root directory. On Windows, your output would instead be C:/Users/jojo. The folder you’re in is called the working directory, and pwd stands for “print working directory.”

The command pwd won’t actually print anything except on your screen. This command is easier to grasp when we interpret “print” as “display.”

OK, we know where we are. But what if we want to know what files and folders are in the jojo directory, a.k.a. the working directory?

Try entering:

$ ls

You should see a number of folders, probably including Documents, Desktop, and so on. You may also see some files. These are the contents of the current working directory. ls will “list” the contents of the directory you are in.

Wonder what’s in the Desktop folder? Let’s try navigating to it with the following command:

$ cd Desktop

The cd command lets us “change directory.”
(Make sure the “D” in “Desktop” is capitalized. ) If the command was successful, you won’t see any output. This is normal —- often, the command line will succeed silently.

So how do we know it worked? That’s right, let’s use our pwd command again. We should get:

$ pwd
/Users/jojo/Desktop

Now try ls again to see what’s on your desktop. These three commands—pwd, ls, and cd—are the most commonly used in the terminal. Between them, you can orient yourself and move around.

Before we move on, let’s take a minute to navigate through our computer’s file system using the command line.

Challenge

Use the three commands you’ve just learned—pwd, ls and cd—eight (8) times each. Go poking around your Photos folder, or see what’s so special about that root / directory. When you’re done, come back to the home folder with

cd ~

(That’s a tilde, on the top left of your keyboard.) One more command you might find useful is

cd ..

which will move you one directory up in the filesystem. That’s a cd with two periods after it.

Compare with the GUI

It’s important to note that this is the same old information you can get by pointing and clicking displayed to you in a different way.

Go ahead and use pointing and clicking to navigate to your working directory–you can get there a few ways, but try starting from “My Computer” and clicking down from there. You’ll notice that the folder names should match the ones that the command line spits out for you, since it’s the same information! We’re just using a different mode of navigation around your computer to see it.

«< Previous Next »>

Example:

Navigating the command line