Expand All

Command Line

The command line is a text-based way to interact with your computer.

In Windows and OS X we use graphical user interfaces (GUIs). GUIs let you operate your computer with pictures and a mouse. We start programs by clicking an icon, look through files and folders in Finder or File Explorer, or pick a command from a dropdown menu.

In the command line, we type commands to run programs and navigate through files and folders. We will need this to run Ruby progams from files. This lesson covers the commands we need for the workshop.

It's helpful to keep Finder (OS X), File Explorer (Windows), or Nautilus (Ubuntu) open during this topic. We'll make files and move between directories. A graphical file browser will help you see the results more clearly.

Goals

  • Learn about the command line

  • Make a new directory

  • Browse some directories on your computer

  • Learn the commands pwd, mkdir, ls, cd and man

Step 1

Open up a command line application on your computer.

On Macs: In Finder, start Applications > Utilities > Terminal, or find the Terminal application through Spotlight (click the magnifying glass in the top right of the screen and start typing 'Terminal')

On Windows: open up Git Shell from your desktop or All Programs menu. Do not use Power Shell! It doesn't support the ssh command that we need.

On Linux: press Ctrl + Alt + T or Find Terminal under the Accessories category of your applications menu.

We will call this "the console" or "the command line", no matter what program you're using.

Consoles will either start in your home directory, the way OS X and Windows open up in your Desktop, or in the directory you ended in during your last console session. We'll talk more about the home directory in a few steps.

Step 2

The first word you type in the terminal is always the name of a program. Many programs are included in OS X and Windows, like ls and pwd. Others are programs you installed, like ruby, irb, or vagrant.

Type this in the terminal:
ls

Hit RETURN or ENTER to run the command.

ls stands for list, and lists the contents of the current directory.

You are in your home directory. You may see directories like 'Documents' or 'Desktop'. If you start up Finder, File Explorer, or Nautilus, you'll see the same files and folders listed.

Step 3

Type this in the terminal:
pwd

pwd means "print working directory". It shows you what directory you're currently in. Directories are also often called folders.

pwd can tell you where you are if you get lost somewhere in your computer.

What directory are you in right now?

Step 4

Type this in the terminal:
cd workspace

cd means change directory. You use cd when you want to move from the current directory into some other directory.

Type this in the terminal:
pwd

Notice how it changed? The working directory ends in '/workspace'.

Step 5

Type this in the terminal:
mkdir railsbridge_ruby

mkdir means make directory. You use mkdir when you want to create a new directory.

Type this in the terminal:
ls

Now the directory "railsbridge_ruby" shows up in the list.

The command line is just one way of manipulating the files on your computer. Try to find the new directory you created in Finder or Windows Explorer.

If you get an error saying the directory already exists, maybe someone did these steps on your computer before. Don't fret.

Step 6

Let's go back to your home directory.

Type this in the terminal:
cd ~

~ is the tilde key. On US keyboards, it's on the top left, next the 1 key, and you have to press SHIFT along with it.

On the command line, the tilde means your home directory, a directory owned by the account currently logged in to the computer. Your home directory might be something like /home/sparklepants (Linux) or /Users/saucyfrank (Mac).

What's your home directory?

Type this in the terminal:
pwd

Remember that you can always get back to your home with cd ~.

Step 7

The dash is a special shortcut that brings you back to your last location, like an "undo" for cd.

Type this in the terminal:
cd -

What command shows you your current directory?

Type this in the terminal:
pwd

You should be back in "railsbridge_ruby\another_directory".

Step 8

There are two tricks to save you some typing. The first one is command history.

Press the up arrow key.

Expected result:
pwd

Press ENTER to run the command.

Keep pressing the arrow key to replay commands. The up arrow moves backward through history, and the down arrow moves forward.

Step 9

The TAB key will autocomplete a command.

Type this in the terminal:
cd ~
ls
cd workspace
cd rai

... and hit TAB.

Expected result:
cd railsbridge_ruby

Autocomplete will be as smart as it can. If you started with cd, it will list directories. If it could be a command, it will suggest commands. When there are multiple matches, it will show you the options.

Type this in the terminal:
p

... and hit TAB. What happens? Hit TAB a second time.

Expected result:
Display all 203 possibilities? (y or n)

When autocomplete is stumped, it'll wait for another hint. It will ask when there are too many possibilities.

Type this in the terminal:
y

There are quite a few commands that begin with p! Autocomplete can remind you when you don't know what all the choices are.

Challenge(s)

Using the command line and your editor, create a new directory that contains a set of text files. These text files will be lists of the various things you want to learn.

For example, you might want to learn various things about ruby and HTML. You could create a ruby.txt file and a html.txt file in the same directory. In each file, enter in a few questions you have about those topics.

Explanation

The command line is an essential tool for computer programmers. While daunting at first, it offers great flexibility in moving around your computer and manipulating files.

There are many, many, many more commands available on the command line than what we've seen here, but these are enough to get you going.

Command summary:

pwd print working directory print the full path to your current directory
ls list directory display the contents of the current directory
cd [directory] change directory make this directory the current directory
man [cmd] manual show the manual for this command. press 'q' to quit.

If your workshop is using a Virtual Machine (ask a TA!) now is the time to take a detour to Using Virtual Machines

Next Step: