the command line

Most of the programs you’re used to running have graphical interfaces. Take the file manager for example (on Windows it used to be called Windows Explorer. I think now it’s called File Explorer):

file manager

Information about your files is displayed to you visually. You can click on things to interact with the program. But did you know there’s another way to get this information? You’ll need to open a program that will let you type commands instead of just using your mouse. It will depend on your operating system.

Show Linux/OS X tutorial

Search or look for an application called Terminal. In Linux, it might be in the Applications -> Accessories menu. It should look something like this:

Linux terminal

Type ls. After hitting enter, you’ll get a list of files and folders (also called directories):

jacob@ubuntu:~$ ls
archive  comp     family      misc   notes     Webcam
church   Desktop  learn_prog  music  pictures

In the file manager, you can double click on a folder to move into it. In the command line, we use the cd (change directory) command. After cd you type the name of the directory you want to move into, e.g. cd documents.

Notice above that one of the directories shown by ls is the music directory. I can move into it like so:

jacob@ubuntu:~$ cd music/
jacob@ubuntu:~/music$ ls
archive  guitar   m4a          originals  playlists  videos
dump     library  old_archive  phone      sheet

Try using ls and cd to move into a different directory on your computer.

Notice that after I used cd, my new “current directory” is displayed right before that dollar sign:

jacob@ubuntu:~/music$

This line is called a “prompt.” It gives you some helpful information and it indicates that the terminal is ready for you to type a command.

If you want to get back to your starting directory, you can type cd ...

jacob@ubuntu:~/music$ cd ..
jacob@ubuntu:~$

Try using cd to move around the directories on your computer.

There are many more commands, but this is enough to get you started. On the next page, I’ll show you how to run a Python program from this command line interface.

Show Windows tutorial

NOTE


There is a video at the end of this tutorial that demonstrates the following information. Try to understand the tutorial first, and then use the video to clear up any questions you have.

The program you need is called the Command Prompt. Open the search bar and type cmd.exe, then run the first one that comes up. On Windows 7 it looks like this (it won’t be too much different on Windows 10):

cmd.exe on Windows

In your little black box, type dir. After hitting enter, you’ll get a list of files and folders (also called directories):

C:\users\jacob>dir
Volume in drive C has no label.
Volume Serial Number is 0000-0000

Directory of C:\users\jacob

  4/7/2017  11:28 PM  <DIR>         .
  5/8/2016   5:11 PM  <DIR>         ..
  5/8/2016   5:11 PM  <DIR>         AppData
  7/1/2016  10:24 PM  <DIR>         Application Data
  5/8/2016   5:11 PM  <DIR>         Contacts
 5/30/2016   3:29 PM  <DIR>         Cookies
 2/14/2017   5:26 PM  <DIR>         Desktop
  5/8/2016   5:11 PM  <DIR>         Downloads
  5/8/2016   5:11 PM  <DIR>         Favorites
  5/8/2016   5:11 PM  <DIR>         Links
  5/8/2016   5:11 PM  <DIR>         Local Settings
  5/8/2016   5:11 PM  <DIR>         NetHood
  5/8/2016   5:11 PM  <DIR>         PrintHood
  5/8/2016   5:11 PM  <DIR>         Recent
  5/8/2016   5:11 PM  <DIR>         Saved Games
  5/8/2016   5:11 PM  <DIR>         Searches
  5/8/2016   5:11 PM  <DIR>         SendTo
  5/8/2016   5:11 PM  <DIR>         Start Menu
  4/7/2017  11:28 PM  <DIR>         Temp
  5/8/2016   5:11 PM  <DIR>         Templates
       0 files                        0 bytes
      20 directories     39,040,172,032 bytes free


C:\users\jacob>

Each line in the output represents one file or directory. On the left you can see what time the file or directory was created. On the right you can see the name. If you opened Windows Explorer, you would see some of these directories (but not all–some of them are hidden, like Application Data).

Do you see this line?

C:\users\jacob>

This line is called a “prompt.” It indicates that the terminal is ready for you to type a command. It also tells you your current location. C:\users\jacob is my default directory.

In Windows Explorer, you can change your location by double clicking on a directory. Observe:

before

I now double click on Downloads:

after

Nothing too earth shattering. But how do you change your location from the command line? With the cd command:

C:\users\jacob>cd Downloads

C:\users\jacob\Downloads>

cd is short for “change directory.” After cd you type the name of the directory that you want to move into. Notice that the prompt now shows my new location. If I type dir, I’ll see the contents of the Downloads directory:

C:\users\jacob\Downloads>dir
Volume in drive C has no label.
Volume Serial Number is 0000-0000

Directory of C:\users\jacob\Downloads

  4/7/2017  11:40 PM  <DIR>         .
  4/7/2017  11:28 PM  <DIR>         ..
  4/7/2017  11:40 PM             0  burma_shave.pdf
       1 file                         0 bytes
       2 directories     39,039,877,120 bytes free


C:\users\jacob\Downloads>

If I want to go back to where I was before, I can type cd ..:

C:\users\jacob\Downloads>cd ..

C:\users\jacob>

Try it out on your own:

  1. Use dir to see what directories you have.
  2. Use cd to move into one of those directories.
  3. Use dir again to view the contents of the new directory.
  4. Use cd .. to move back to the first directory.

Stuck? Watch this video (I recommend full screen. And increase the video quality if it’s fuzzy):


There are many more commands, but this is enough to get you started. On the next page, I’ll show you how to run a Python program from this command line interface.


previous: your first programming language next: installing python