class logo

CS 122 - The Terminal

We talked in class today about using the Terminal program to run our python scripts. Here's a mini reference on how to use it. We'll talk more about the terminal as the term progresses

There are lots of tutorials about the terminal on the web, but most of them aren't very good. I looked around and decided that this one isn't too bad. If you want more information about the terminal, check it out.

You can launch the terminal using Spotlight: hit command-space, then start typing the name "Terminal".

The terminal is a command-response environment. You type in your command, ending it with the return key; and then the result of your command is displayed. Some commands display results immediately, while others may ask for additional information before displaying the response.

You can specify arguments to a command by separating them from the command (and each other) with spaces. Because the space character is used as a separator, things get confusing if you try to save your file with a space in the name.

The terminal can be used for lots of things, in this class we will use it primarily for launching our python scripts.

Here is a list of some of the most common commands you will need to use. Arguments in italics represent placeholders for you to type an argument relevant to you (for example, type "cd Desktop" to change to the desktop directory, not "cd newdir")

Command Description
pwd prints the current working directory
ls list the files in the current working directory
cd change the working directory to the home directory
cd newdir change the working directory to the one specified as the "newdir" argument
cd .. change the working directory to the directory containing the current working directory
mv fileone filetwo move the file named fileone to filetwo. this essentially renames the original file to filetwo
cp fileone filetwo copies the file named fileone to the new name filetwo
clear clears the screen

To run a python program, you first need to get to the directory where the python program is saved, then use the python command to run your program. If you are using a Mac and your python progam is saved on your desktop as a file named myProgram.py, you would execute the following commands

cd Desktop python myProgram.py

You can also run python in an interactive mode by executing the python command with no arguments:

python