Josh Hepworth

An Introduction to the Command Line
Learning to Program Software

Why do I need to know about this?

The command line interface is a low level, text only way of interacting with a computer. GUI applications today are built as command line applications were built decades ago. Unlike GUI applications the way you interact with a single command isn’t obvious. There are no buttons or labels or obvious clues about how to use them.

Still these tools are popular in certain circles. They often execute quicker and are easier to write than their GUI counterparts. For example, Excel might not open a 2 million row file, but a command line application would be able to filter the data, manipulate it, or whatever it needs to do with much less hassle.

Command line tools generally deal with text files, and this specialty has made them well suited for assisting in a programmer’s workflow by providing powerful code generation tools, easy ways to run local (on your machine) web servers when developing web applications, and

What is it?

The command line interface does a pretty good job of describing itself. It’s an interface where you input text commands to generate output. On modern computers, it usually exists as an application you can launch, such as Terminal.app on OS X. The interface is simple, and you will only use your keyboard to interact with it. (You can use a mouse in some contexts, but that’s some sugar your OS is adding for you.)

Interacting with a CLI (command line interface) follows a simple loop.

  1. Type in a command at the prompt. (Probably a ends with a $.)
  2. Press enter.
  3. The computer executes the command.
  4. The computer shows you the command’s output (if any).
  5. The computer prompts you for more input.

Terminal.app Command Prompt on OS X

The above shows the default Terminal.app on OS X with the default prompt and my block cursor. All of this can be configured and changed, but I want to go through this with the defaults that most first time Terminal.app users will see.

Your computer comes with a quite a few default commands. You can look at a fairly complete list online, but that’ll probably look like a bunch of garbage at the moment. For now, I’ll introduce you to a few commands.

Basic File System Commands

The following commands allow you to "move" around in your computer's file hierarchy in the Terminal. When you're in Terminal you're always "in" a directory. This current directory is called your working directory and you can view it by typing pwd into Terminal.app and hitting enter. Think of pwd as "Print Working Directory".

The pwd command on my computer

Running pwd told me that I'm currently in the /Users/Josh directory. Let's see what's in the directory that I'm currently in with the ls command, which we'll think of as "List Stuff".

The ls command on OS X

We can see that this is the same information that Finder.app shows us when we're in our home directory.

Finder.app view of the ls command

We can also move our current working directory to one of the directories listed here by entering cd DIRECTORY_NAME. I'm going to type cd Pictures, and then pwd so that I can see I changed into that directory.

The cd command

That was the first time that we gave an argument to a command. In my example "cd" was the command, and "Pictures" was the argument. You can give the "cd" command any argument, but it will only understand what to do if you pass it a path to a directory. The command line also requires you to treat spaces in a special way. By default, spaces are used to separate arguments being sent to a command, so if I actually want to cd to a directory with a space in its name, I need to explicitly state that this space is part of a single argument. To do this I put the argument in double quotes, cd "VirtualBox VMs".

Other commands will take different arguments, but you'll be doing a lot with files and folders in the command line. You'll want to get used to moving around your file system and referencing other files.

Next time we'll talk about options that can be given to commands, as well as a popular place to get new commands on OS X, Homebrew.

Bonus Points

If you want to learn more about a command you can use the man (manual) command and pass in the name of another command as an argument. To learn about ls you would type man ls. man has a not-so-intuitive viewer. You can scroll down by typing 'j', up by typing 'k', and quit by typing 'q'.