Contents

Environment

A command line, or more technically, a command line interface (CLI), is a text-based method of interacting with a computer. CLIs were the original way to interact with computers, before there were mice or touch pads. It may seem archaic, but it's actually a very efficient, time-tested way to interact with your computer, at least when you're programming.

The most popular command line command sets are based on the Unix operating system. Today's Linux systems shadow Unix and Mac OSX actually runs on top of Unix. Windows' CLI is DOS (Disk Operating System), however, in order to comply with the number of users familiar with Unix, Windows now ships a program called PowerShell that offers many of the same commands as Unix.

Now, lets get you set up so you can see what we're talking about. Following the directions below based on your operating system.

Windows setup

In Windows 7 or 8, search your applications for a program called PowerShell. That should bring up something that looks like:

PowerShell screenshot.

Look for a blinking underline; it should appear just to the right of the > character. The blinking underline is called the cursor and the text to the left (in the screenshot above, the text PS C:\Users\hfeild>) is called the command prompt, or prompt for short.

Mac OSX setup

In Mac OSX, search your applications for a program called Terminal. Spotlight is probably the easiest way to search for it—you can open Spotlight by pressing the key combo Command+Spacebar. A search box should appear in the upper right corner of your screen. Type "Terminal" and select the Terminal application from the search results. When opened, it should look something like this:

Mac Terminal screenshot.

Look for a filled rectangle; it should appear just to the right of the $ character (you may have a different character). The filled rectangle is called the cursor and the text to the left (in the screenshot above, the text [09:21:00][hfeild ~]$) is called the command prompt, or prompt for short.

Linux setup

There are lots of Linux variants. If you're using linux, you probably know how to open a terminal. For this example, lets suppose you're running Ubuntu. Press the key combo Ctr+Alt+T or search applications for Gnome Terminal. You should see a window like the following:

Gnome Terminal screenshot.

Look for a blinking, filled rectangle; it should appear just to the right of the $ character (you may have a different character). The blinking rectangle is called the cursor and the text to the left (in the screenshot above, the text [09:21:00][hfeild ~]$) is called the command prompt, or prompt for short.

(Back to top)

Basics

Now that you know how to access a CLI on your system, lets use it! One thing you want to always keep in mind is, you cannot use your mouse with a CLI—it's entire keyboard based. To move left and right, you need to use the left and right arrow keys. This can be tedious, but you'll soon grow accustomed to it.

Lets start by considering what the CLI is. It's really a way to run programs and navigate through your file system. So in many ways, it's a glorified, text-based File Explorer (e.g., Mac Finder or Windows Explorer). Most of the commands we will learn about are programs that will help you navigate your file system.

pwd

The first command we'll learn about is pwd, short for "path to working directory". At your CLI prompt, type in pwd. You will get a path to your current folder (called a directory in CLI-speak), like this:

Mac/Linux
$ pwd
/Users/hfeild
PowerShell
PS C:\Users\hfeild> pwd

Path
----
C:\Users\hfeild

(Note: in PowerShell, this will look slightly different). A path is a sequence of directories, separated by slashes. In Unix-based systems, a forward slash (/) is used. In Windows, backslashes (\) are used, but PowerShell lets you use either, which is convenient. Path's come in two forms: absolute and relative. Absolute paths start with the root directory. On Unix-based systems, the root is a single slash: /. In Windows, it is the drive letter, usually C:\ (paths that start with another drive letter are also absolute paths, e.g., E:\). The path displayed by the pwd command is an absolute path. It tells us exactly what set of directories we need to follow from the root to get to the current directory the CLI is sitting in.

We'll learn about relative paths in a couple sections.

ls

Knowing the path to the current directory is great, but what if we want to know what files and subdirectories reside in the current directory? We can list these by using the ls command (list directory contents). Here's what it looks like:

Mac/Linux
$ ls
Applications   Google Drive   Public         Win7Shared     shared
Desktop        Library        Qt             bin            tmp
Documents      Movies         Sites          csc160
Downloads      Music          Ubuntu One     foo
Dropbox        Pictures       VirtualBox VMs research
PowerShell
PS C:\Users\hfeild> ls


    Directory: C:\Users\hfeild


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d-r--         9/25/2013  10:15 AM            Contacts
d-r--          1/3/2014  10:47 PM            Desktop
d-r--         9/25/2013  10:15 AM            Documents
d----        10/27/2013   4:27 PM            DOS
d-r--        11/11/2013   3:52 PM            Downloads
d-r--         9/25/2013  10:15 AM            Favorites
d-r--        10/27/2013   2:26 PM            Links
d-r--         9/25/2013  10:15 AM            Music
d-r--         9/25/2013  10:15 AM            Pictures
d-r--         9/25/2013  10:15 AM            Saved Games
d-r--         9/25/2013  10:15 AM            Searches
d-r--         9/25/2013  10:15 AM            Videos

Note that your files and sub-directories will differ. You can see that PowerShell returns additional information, including whether a "file" is a directory or not (denoted by the leading d). You can get similar output in Mac/Linux by passing the -l flag to the ls command, like this:

Mac/Linux
$ ls -l
total 8
drwx------   4 hfeild  staff   136 Dec 20 11:42 Applications
drwx------+ 20 hfeild  staff   680 Jan  3 22:40 Desktop
drwx---r-x+ 16 hfeild  staff   544 Dec 23 09:32 Documents
drwx------+ 45 hfeild  staff  1530 Jan  4 10:09 Downloads
drwx------@ 20 hfeild  staff   680 Jan  2 17:24 Dropbox
drwx------@ 21 hfeild  staff   714 Dec 23 11:00 Google Drive
drwx------@ 56 hfeild  staff  1904 Jan  3 13:17 Library
drwx------+  4 hfeild  staff   136 Oct 25 00:20 Movies
drwx------+  4 hfeild  staff   136 Sep 15 16:01 Music
drwx------+  8 hfeild  staff   272 Dec 11 09:07 Pictures
drwxr-xr-x+  4 hfeild  staff   136 Sep  6 09:23 Public
drwxr-xr-x  13 hfeild  staff   442 Dec 23 09:23 Qt
drwxr-xr-x  10 hfeild  staff   340 Dec 14 16:36 Sites
drwxrwxr-x   8 hfeild  staff   272 Sep 29 10:51 Ubuntu One
drwxr-xr-x   4 hfeild  staff   136 Sep  7 10:50 VirtualBox VMs
drwxr-xr-x  29 hfeild  staff   986 Jan  4 09:20 Win7Shared
drwxr-xr-x  15 hfeild  staff   510 Dec 23 07:57 bin
drwxr-xr-x   6 hfeild  staff   204 Dec  1 21:29 csc160
drwxr-xr-x   4 hfeild  staff   136 Dec 14 15:40 foo
lrwxr-xr-x   1 hfeild  staff    32 Dec 14 20:03 research -> /Users/hfeild/Documents/research
drwx---r-x   5 hfeild  staff   170 Sep 20 08:38 shared
drwxr-xr-x   2 hfeild  staff    68 Dec 15 21:31 tmp

The -l flag is an example of what we generically call a command line argument or command line parameter (we'll use argument and parameter interchangeably). Command line arguments are whitespace-separated strings passed to a command (in this case, ls). You can pass in as many arguments as you want, though its up to the program whether or not it uses them, or it may even fail with an error that an argument you passed in is unrecognized. We'll see some other examples of command line arguments.

You can also pass in absolute or relative paths to ls. For example, to list all of the files in /Users, we can do the following:

Mac
$ ls /Users
hfeild

cd

The cd command is short for change directory. You can invoke it without any commands, in which case you will end up back in your home directory (e.g., on my Mac, that's /Users/home/hfeild) or you can pass it a relative or absolute path as a command line argument. For example, to change into /Users/home on a Mac, we can type:

Mac
$ cd /Users/home
$ pwd
/Users/home

By entering the command pwd right after cd, we can see that the working directory has been changed to /Users/home. From here, if we type cd without any arguments and then check the path to the working directory again, we'll get:

Mac
$ cd
$ pwd
/Users/home/hfeild

Lets talk about relative paths for a minute. Relative paths are paths that are relative to the working directory. So looking at all of the directories listed in my home directory, I can change into one of them by just supplying the directory name. E.g., issuing the command cd Desktop from my home directory will change me into /Users/home/hfeild/Desktop:

Mac
$ cd
$ pwd
/Users/home/hfeild
$ cd Desktop
$ pwd
/Users/home/hfeild/Desktop

Relative paths can consist of more than one nested directory. For example, lets say that inside of my Desktop directory, I have a subdirectory named notes. The absolute path would look like: /Users/home/hfeild/Desktop/notes; the relative path from my home directory would look like: Desktop/notes. This example should shed some light on it:

Mac
$ cd /Users/home/hfeild/Desktop/notes
$ pwd
/Users/home/hfeild/Desktop/notes
$ cd
$ pwd
/Users/home/hfeild
$ cd Desktop/notes
$ pwd
/Users/home/hfeild/Desktop/notes

To drive home the point, absolute paths can be used to navigate to a directory no matter the path of the current working directory. Relative paths, however, must be relative to the current working directory.

A nice little shortcut is the use of ~ (called a tilde, and pronounced "til-da") in place of the path of your home directory. So, on the command line ~ is equivalent to /Users/home/hfeild in Mac OSX, C:\Users\home\hfeild in Windows, and /home/hfeild in Linux. E.g.,

Mac
$ cd ~/Desktop/notes
$ pwd
/Users/home/hfeild/Desktop/notes
(Back to top)

Advanced navigation

There are many relatively common features of CLIs that make navigating a breeze. We outline some of them here:

Canceling a command

Sometimes you may find that you've started typing something on the command line and then decide you'd like to start over. You could just backspace all the way back to the beginning, but that's a lot of work. You can delete the whole thing by using the key combo Ctrl+c. That will cancel your old command. This is also the combo you use to quit a program that's running (you might need to use it when you run one of your own programs).

Command history

Your CLI will keep track of your recent command history. To cycle through your commands in chronological order, use the up and down arrow keys. Every time you hit the up arrow, you'll go back another command in your history. Hitting down will go forward one command.

Tab completion

It can be annoying and tedious to have to type everything out, especially if you are trying to change into a directory with a long path. Luckily, most CLIs have something called tab completion. As you start to type a command or a path, just press the tab key and you'll start getting suggestions. This behaves a little differently in Linux/OSX than in Windows. In Linux/OSX, tab completion will do one of two things: 1) complete the command or directory name for you or 2) if there is more than one command or directory name, that shares the same prefix, it will complete up until the point where one of them diverges. In the latter case, you can double-tap tab again to see all the options (they'll be printed to the screen).

Here's an example. Say that My desktop contains the following subdirectories:

Linux/Mac
$ ls ~/Desktop
notes classes-2013 classes-2014

If I want to change into the notes directory, then you can start typing cd ~/Des, then hit tab. In Linux/OSX, you should now see cd ~/Desktop appear (i.e., ktop is automatically filled in for you). Keep typing, adding in /no, then hit tab again. You should see the tes letters appear, giving you cd ~/Desktop/notes.

Now lets say you want to go into the classes-2014 directory. Notice that there's another directory with the same name up until the last character. If we type in cd ~/Desktop/c and then hit tab, we'll get the following: cd ~/Desktop/classes-201. The CLI doesn't know whether you want the one with the 3 or 4 at the end, and it doesn't bother guessing. Lets pretend that you didn't know what the options were. You can double-tap the tab key. That will give you a list of options. In our case, it'll look something like this:

Linux/Mac
$ cd ~/Desktop/classes-201
classes-2013/ classes-2014/

Now you can type the remaining character.

Windows handles tab completion a bit differently. First, it will auto expand your path (so ~\Desktop becomes C:\Users\home\hfeild\Desktop). Then, it will choose one of the files or subdirectories that matches the prefix you gave. You can cycle through options by pressing tab again.

Copy/Pasting

Copying and pasting is different on each system. Mac is the simplest, as it uses the same short cuts as every other Mac app: Cmd+c to copy and Cmd+v to paste.

Linux requires you use Ctrl+Shiftc to copy and Ctrl+Shiftv to paste.

In PowerShell, you can copy text by highlighting it (highlighting is kind of weird, so prepare yourself) and then right click your mouse. To past text, be sure nothing is highlighted, and then right click. That's correct, right clicking is used for both copying and pasting.

Home/End/Back/Forward

Navigating a long command can be tricky. Here are a few nice shortcuts for moving within a line in a CLI:

Some of these may not work all of the time. If you're having trouble, search the Web for a workaround.

(Back to top)

Common commands

CommandPurpose
cd E.g., cd ~/Desktop
Changes the working directory to the specified directory. Directory paths can be absolute or relative. If no arguments are given, the destination path is assumed to be the user's home directory (~/).
ls E.g., ls ~/Desktop
Lists the files and subdirectories of the specified directory. Directory paths can be absolute or relative. If no arguments are given, the contents of the working directory will be listed. To list information about each file/directory, include the flag -l: ls -l ~/Desktop.
pwd E.g., pwd
Displays the path of the working directory.
mkdir E.g., mkdir ~/Desktop/notes
Creates a new directory, if it does not exist. The path of the new directory can be relative or absolute. Multiple paths can be included as separate arguments. To create all directories on the path that do not yet exist, include the -p flag: mkdir -p ~/Desktop/notes/a/b/c.
cp E.g., cp program1.cpp program2.cpp
Makes a copy of the first listed file (program1.cpp in the example) and gives it the name of the second argument. You can copy multiple files and directories into a destination directory by using the -R flag (short for recursive), listing all of the files/directories to copy, and ending with the directory to copy into, e.g.: cp -R ~/Documents/notes program1.cpp ~/Documents/notes2.
mv E.g., mv program1.cpp program2.cpp
Renames the first listed file (or directory) (program1.cpp in the example) and gives it the name of the second argument. You move multiple files and directories into a destination directory by listing all of the files/directories to move, and ending with the directory to move into, e.g.: mv ~/Documents/notes program1.cpp ~/Documents/notes2.
(Back to top)

Exercises

  1. In your operating system's file viewer (e.g., Finder on Mac or Windows Explorer), create a subfolder in your Documents folder named command-line-exercises. Now open your CLI and navigate to that directory. Issue the pwd command. What do you see?
  2. Using the CLI and not Finder/Windows Explorer, create a new subdirectory in the command-line-exercises directory named exercise-2. Now open Finder/Windows Explorer. Do you see the directory you just created with from the command line?
  3. Create a blank text file (using Notepad/TextEdit/Sublime Text/jEdit) and save it in the command-line-exercises directory as exercise-3.txt. From the CLI, list the files in the command-line-exercises directory and see if the text file is there.
  4. Using the CLI, copy the exercise-3.txt file into the exercise-2 subdirectory. List the contents of that directory to make sure it's there.
  5. Using the CLI, rename the exercise-3.txt file in the main command-line-exercises directory to exercise-5.txt. List the contents of the command-line-exercises directory to see if the renaming was successful.
(Back to top)