Commanding the command line interface

Jason Slusarchuk
Ambitions of a Recovering Salesman
8 min readAug 1, 2017

Before you’re able to start the LaunchSchool program in earnest, there are a couple of preparatory courses that you need to complete:

The first is an introduction to LaunchSchool itself and their teaching methodology (mastery based learning; something I’ll likely write about in more depth at a later time).

The second is structured around setting up your development environment, getting familiar with a few of the most common tools that developers use, and learning some basic programming concepts. As I just completed LaunchSchool’s Introduction to the Command Line workbook, I’ll be sharing a few simple examples of how the command line interface works. And if you have no idea what a command line interface is, no worries, I’ll explain that too!

What is the command line?

Simply stated, the command line interface (CLI) is just another way for you to interact with your computer and give it commands. It’s completely text-based, and through it, you can do pretty much anything you’d normally do with a computer including locating folders, opening files and running programs (to name just a few actions). And although you probably don’t come across the command line interface of your computer’s operating system often, it’s definitely there. Whether you’re using a Mac, a PC, or even a mobile device, they all have an operating system at their core with a command line interface available for you to access.

How do I use the command line interface?

Tip: The first thing you’ll want to do is open the command line interface on your computer. On a modern Mac, you can open the Finder window, then navigate to Applications, then to Utilities, and then open the application called Terminal. If you’re using a PC it will depend on which version of Windows you’re using, so here’s a quick guide that I found online which you follow.

For the purposes of this introduction to the command line interface, I’ll be using a Mac. Some of the instructions I provide may not work the same on a PC running Windows.

These days, most of us are familiar with using a graphical user interface (GUI) in order to navigate our devices and issue commands: we click, tap and swipe at icons, files, images and other visual objects present on screen. But before there were graphical user interfaces, there was ONLY the text-based command line interface! The horror!

It might seem intimidating at first, but once you get a few simple commands under your belt, you’ll discover it’s actually not all that hard to use. In some cases, once you get proficient with the command line interface, you can actually get your tasks done faster than if you were to use the graphical user interface!

The command line interface on a Mac

It works like this: 1) you enter a command into the prompt, 2) the computer processes the command, 3) control of the prompt returns to you so that you can enter the next command. (In some cases, like when you launch a program, you may need to exit the program before returning to the prompt.)

Let’s give it a try. Open up the command line interface on your computer and enter the command ls into the prompt, then hit return.

You should see a list of folders (a.k.a. directories) associated with your user profile. Recognize any of them? These are the same directories you’d find if you were to open up the Finder window on your computer.

Now what if you wanted to locate a file in one of these directories? Well, let’s think about how we’d explain that to a computer. First, we’d probably want to tell the computer which directory we want to focus on. Then, we’d need to tell the computer to give us a list of all of the files in that directory so that we can find the one we’re looking for.

Let’s suppose we’re looking for a file in the applications directory listed below. Try using the command cd applications.

Can you spot what changed after we entered ‘cd applications’ into the prompt?

Hmm, doesn’t look like anything happened, does it? Look closer. Do you notice how the text preceding the prompt (jasons$) changed from a ~ to the word applications? That means we’re now in the applications folder.

Tip: The ~ is a symbol representing your user’s home folder

Now that we’re in the right folder, how do you suppose we’d get a list of the files and folders within the applications folder? With the ls command, of course! Try entering ls into the prompt again, then hit return.

The applications listed in your folder may be different then the applications listed in my folder

Ah ha! Now we have a list of the files and directories available to us within the applications directory. Pretty easy right? But what if you just remembered that the file you’re looking for is actually in the pictures directory? What do you think would happen if you tried entering cd pictures into the prompt?

Womp womp!

Unfortunately we get an error telling us that there is no such file or directory called pictures in the applications directory. The reason this happened is that the cd command will only look for files or directories within the folder you’re currently in. If we want to navigate to a directory somewhere else on the computer, we need to tell the computer exactly where to look.

Try this instead: cd ../pictures

That worked! Notice that we are now in the pictures directory? (Remember, you can tell what directory you’re in by looking at the word or text preceding the prompt, in this case pictures) But why did it work this time?

The secret is in the ../ that we prepended to the directory name. This bit of text tells the computer to go back up one folder first and then to look for the pictures folder.

Tip: If all you want to do is go back up one folder, you can just use the cd .. command without the forward slash.

Ok, so we’re doing pretty good now. We’re in the pictures directory and now we need to find the file we’re looking for. In this case, I’m looking for a file called ‘fancy_cat.jpeg’. And how do we get a list of the files in the pictures directory again? You’re right, with the ls command!

And there it is! We see that the fancy_cat.jpeg file is indeed located within the pictures directory, among a few other files.

Now that we’ve found the file we’re looking for, we probably want to do something with it. In this case, we know the file is an image because of the .jpeg extension. How about we take a look a the photo? Sounds like a reasonable plan, right? But wait a minute, we’re using the command line interface, not a graphical user interface. We’re going to need to use a program that can open .jpeg files in order to properly view the image.

There are many programs that can be used to view image files. Photoshop, GIMP and MS Paint, to name just a few. On a Mac, we have a basic image viewing app called Preview, so let’s use that.

Try entering the following command into the prompt: open -a preview fancy_cat.jpeg (try replacing 'fancy_cat.jpeg' with the name of an image file on your own computer.)

Assuming you used a file available to you, the computer should accept the command and return control of the prompt to you. And did you notice what else happen? The computer opened up the Preview application and a copy of the image file!

Fancy, right?

Awesome! But let’s back up a second because there a few things that happened within the single command we just gave. Let’s break down the entire command to see exactly what happened.

First we used the command open. This tells the computer that we want to open something. But remember, computers need you to be really specific. What do you want to open, exactly? In this case we want to open an application, and we can tell the computer we want to open an application by using the -a flag. Next, we need to tell the computer what application to open. In this case it’s the Preview application, so we can use the keyword preview. And finally, we need to tell the computer which file we want to open within the application. In this case we want to open the fancy_cat.jpeg file, so we provide the input, fancy_cat.jpeg.

All together you could say it reads a bit like this: open an application called preview to view the file fancy_cat.jpeg.

Kind of makes sense now, doesn’t it?

Taking your skills to the next level

Well congratulations, you’ve just navigated your computer’s folder system, found a file and opened it, all through the command line interface without the help of a mouse. Nice!

There is of course much more to learn about command line interfaces, but with the knowledge gained in this short introduction, you’ve taken the first few steps towards becoming the master of your computer and a more efficient programmer.

If you’d like to learn more, I’d recommend completing the Introduction to the Command Line workbook. It’s completely free and goes into a lot more detail than I cover here.

Happy coding!

--

--