T-Th 9:05
or
T-Th 11:15
in Olin 155

CS 1110: Introduction to Computing Using Python

Fall 2012

Using Text-Based Commands

Python first became popular as a scripting language, which is used to automate repetitive tasks on a computer. As a scripting language, it is heavily geared toward being used in text-based command shell. In order to learn how to get the most of out Python, you need to learn how to use your command shell.

Command shells are very operating system dependent. They are called different things depending on whether you are using Windows, Macintosh, or Linux. The commands that you are allowed to type in them also depend on your choice of OS. Below we describe how to use the command shell in each of the popular computer operative systems.

The specific commands on the various operating systems are similar, but they can also be very, very different (particularly between Windows and OS X/Linux). If you have never used a command shell before, we highly recommand that you pick one operating system and use it for the entire semester; otherwise, this can get very confusing. In particular, if you have a Macintosh laptop, we suggest that you bring it to the lab sections, as the lab computers are all Windows machines.

As with the Python installation instructions, this tutorial has a very much "do this, do that" flavor to it. This is to give you a quick introduction to the command shell. But we are not expecting you to master the command shell immediately. You will get a lot of practice with it over the course of the semester.

[Windows]  [Macintosh]  [Linux


Windows

In Windows, the command shell is called the Command Prompt. It can be found in the Accessories folder of your Start Menu, as shown to the right.

When you start up the Command Prompt, you get a Window that looks something like the following. At any given time, the bottom line of the Command Prompt is the working directory followed by a > symbol. This > symbol is the prompt that gives the Command Prompt its name. It is a cue for you to type something into the shell.

To get the Command Prompt to do something, simply type in a command, and hit Return. The shell will then process the command, either doing something or printing out an error message. When done, it will present the prompt again, ready for you to type in a new command.

The Command Prompt has a property very similar to the interactive shell in that you can use the up and down arrows to revisit commands that you typed in previously.

The most important thing to understand about the Command Prompt is that it works like the Windows Explorer. At any given time it is open to a specific folder (or directory) on your computer. We call the folder that is currently open in the shell the working directory. Whenever you start up the command prompt, the working directory will be your "home directory". This is the folder with the same name as your user account, (not the Desktop folder, but the folder that contains the Desktop folder).

When working on a Python assignment, you want to make sure that the working directory is the directory that contains the .py files you are currently editing. Many a student has found themselves editing a .py folder while testing one (of the same name) in a different folder. You might be tempted to just put everything in your home directory. However, this is a bad idea and as the folder will get very cluttered as the semester progresses.


Navigating Directories

Because you often need to change your working directory, the two most important commands to know in Windows are dir and cd. Typing in dir displays the current working directory as well as all of its contents. An example of the dir command is shown below.

The cd command is an abbreviation for "change directory". It is how you move from one folder to another. When you type this command into the Command Prompt, you must give it two things: the command cd and the name of the folder you wish to go to. Using the example above, suppose we wish to switch the working directory to Desktop. Then we would type

    cd Desktop
Try this out and then type dir; see the difference?

There are a couple of important tricks to know about the cd command.

Backing Out of a Directory

The simplest form cd can only move to a folder that is "sees" (e.g. is a folder inside the working directory). If you change to directory (such as Desktop), you can no longer see the original directory (your home directory); it is outside of the current working directory. So how do you back-out if you go into a folder by mistake?

The solution is that there is a special folder called "..". This refers to the folder that contains the current one. Type

    cd ..
and see what happens. If you typed it just after moving into the Desktop folder (from the previous example), then you should be back in your home directory.

Combining cd .. with regular uses of the cd command are enough to allow you to move up and down the directory hierarchy on your computer.

Tab Completion

If you are new to the Command Prompt, you might find yourself quickly getting tired of all the typing that you have to do. Particularly when you have a directory with a very long name. A slight misspelling and you have to start all over again.

Fortunately, Windows has tab completion to speed things up. Go to your home directory and type (but do not hit Return)

    cd D
Now hit the tab key. See what happens? Windows turns "D" into the first folder that it can find that starts with that letter (which is likely to be Desktop, and not Documents, as it comes first alphabetically).

Changing Multiple Directories at Once

Suppose you are currently in the your home directory; you want to move to the folder "Favorites" which is inside of "Documents". You could do this with two cd commands. But to do it with a single command, you just connect the folders with a \, as follows:

    cd Documents\Favorites

When you combine this with .., you can do some rather clever tricks. Suppose you are currently in the Desktop directory, and you want to move in the Documents directory (which is contained in your home directory). You can do this with the command

    cd ..\Documents

We refer to these expressions as paths; they are are a "path" from the working directory to the directory that you want to go to.

Absolute Paths

The paths that we have shown you are more properly called relative paths. They show how to get from the working directory to your new directory. The correct path to use depends on exactly which directory is the current working directory.

Absolute paths are paths that do not depend on the working directory; instead they depend on the disk drive. They always start with name of the drive. For example, suppose you inserted a USB drive into the computer, and you wanted to open that drive in the Command Prompt. The USB drive will (typically) be the the E: drive, so you simply type

    cd E:

You can combine this with the \ symbol to move anywhere you want on the USB stick. If the USB stick has a folder called "Python" on it, simply type

    cd E:\Python

Any time that you need to change disk drives, you need to use absolute paths. Suppose your user account is called "Sally". Then to return to your home directory from the USB drive, type

    cd C:\Users\Sally

Folder Names with Spaces

The Command Prompt breaks up the commands that you type in by spaces. That means that if you have a folder with spaces in the name, it will break it up into references to two different folders. For example, suppose you have a folder called "Python Examples", and you type

    cd Python Examples
You will get an error saying that Windows cannot find that path.

To solve the problem, put the directory in quotes. The following should work correctly.

    cd "Python Examples"
If you are changing multiple directories then you need to put the entire path in quotes (not just the folder). For example, if you want to go to "Program Files", type
    cd "C:\Program Files"

Manipulating Files

The Command Prompt allows you to do everything that Windows Explorer can do (and more). Some of the things that you might find the Command Prompt for is making folders, moving files, and deleting files. However, it is important to understand that none of this is necessary. For this class, you never need to understand how to do anything other than navigate directories. You can do everything else in Windows Explorer (or some other program) if you wish.

Make a Directory

To make a new folder or directory, use the command mkdir followed by the name of the new folder. For example:

    mkdir MyFolder
The new folder will appear in the current working directory.

You can also delete a directory with the rmdir command. For example, to delete the folder we just made, type

    rmdir MyFolder
The command prompt will only delete empty directories. If there is anything in a directory, it will not let you delete it. You have to delete the contents first.

Move (or Rename) a File

You move files with the move command. The way this command works is that you give it two file names. It searches for a file with the first file name; once it finds it, it makes a copy with the new file name and then deletes the original.

For example, suppose you wanted to rename the file test.py to assignment3.py. Then you would type

    move test.py assignment3.py
(this by the way, illustrates why paths cannot have spaces in them).

If the second filename is path to a file, then it will move the the file into the correct directory. For example, suppose we now wanted to move assignment3.py to the Desktop (which is a folder in the current working directory), and rename it completed.py. Then we would type

   move assignment3.py Desktop\completed.py
If we want to keep the name as assignment3.py, we could shorten this to
   move assignment3.py Desktop
In this case, the Command Prompt will move assignment3.py into Desktop, but keep the name of the file unchanged.

Copy a File

The move command will always delete the original (name of) the file when it is done. Sometimes we want to make a copy of a file. We do that with the copy command. Suppose that assignment3.py is in the working directory and we want to put a copy on the Desktop without deleting the original. Then we would type

   copy assignment3.py Desktop\assignment3.py

Delete a File

Files are deleted with the del command. In our running example, to delete the file assignment3.py, you would type

    del assignment3.py

Be very careful with this command. It completely erases the file. It does not move the file your Recycle Bin. You cannot retrieve a file deleted this way.


Getting Help

There are hundreds of resources out there on how to learn to use the Command Prompt in Windows. If you want to learn more, we suggest this tutorial as a starting point.

If have are having difficulty with the Command Prompt, please see one of the course staff or consultants. They are available to help.


Macintosh

On the Macintosh, the command shell is called the Terminal. If it is not in your Dock (where it belongs!), it can be found in the Applications > Utilities folder as shown below.

When you start up the Terminal, you will get some message about the "last login" (a hold over of the days in which Terminals were used to connect machines over the network) followed by a line with a cursor that looks like a box. The left side of the line will depend on your settings, but if you ran the bashrc installer from the Python instructions, the last symbol should be >. This > symbol is called the prompt, and it is a cue for you to type something into the shell. The image below gives more details about the nature of the prompt.

To get the Terminal to do something, simply type in a command, and hit Return. The shell will then process the command, either doing something or printing out an error message. When done, it will present the prompt again, ready for you to type in a new command.

The Terminal has a property very similar to the interactive shell in that you can use the up and down arrows to revisit commands that you typed in previously. If you used the bashrc installer, you will notice a number in a box, just to the left of the prompt. This number tells you the number of the current command, and indicates how far you can go back with the up-arrow to old Terminal commands.

The most important thing to understand about the Command Prompt is that it works like the Windows Explorer. At any given time it is open to a specific folder (or directory) on your computer. We call the folder that is currently open in the shell the working directory. Whenever you start up the command prompt, the working directory will be your "home directory". This is the folder with the same name as your user account, (not the Desktop folder, but the folder that contains the Desktop folder). If you used the bash installer, the current folder is listed to the left of the box with numbers, after the name of your computer. The symbol ~ is a special symbol indicating your home directory.

When working on a Python assignment, you want to make sure that the working directory is the directory that contains the .py files you are currently editing. Many a student has found themselves editing a .py folder while testing one (of the same name) in a different folder. You might be tempted to just put everything in your home directory. However, this is a bad idea and as the folder will get very cluttered as the semester progresses.


Navigating Directories

Because you often need to change your working directory, the two most important commands to know in the Terminal are pwd, ls, and cd. Typing in pwd displays the current working directory. The command ls lists the contents (files and folders) in the working directory. An example of these two commands is shown below.

The cd command is an abbreviation for "change directory". It is how you move from one folder to another. When you type this command into the Terminal, you must give it two things: the command cd and the name of the folder you wish to go to. Using the example above, suppose we wish to switch the working directory to Desktop. Then we would type

    cd Desktop
Try this out and then type ls; see the difference?

There are a couple of important tricks to know about the cd command.

Backing Out of a Directory

The simplest form cd can only move to a folder that is "sees" (e.g. is a folder inside the working directory). If you change to directory (such as Desktop), you can no longer see the original directory (your home directory); it is outside of the current working directory. So how do you back-out if you go into a folder by mistake?

The solution is that there is a special folder called "..". This refers to the folder that contains the current one. Type

    cd ..
and see what happens. If you typed it just after moving into the Desktop folder (from the previous example), then you should be back in your home directory.

Combining cd .. with regular uses of the cd command are enough to allow you to move up and down the directory hierarchy on your computer.

A different but related command is

 cd ~
The ~ is a shortcut for your home directory. Typing that at any time will put you back in your home folder. This is very helpful should you ever get lost while using the Terminal.

Tab Completion

If you are new to the Terminal, you might find yourself quickly getting tired of all the typing that you have to do. Particularly when you have a directory with a very long name. A slight misspelling and you have to start all over again.

Fortunately, OS X has tab completion to speed things up. Go to your home directory and type (but do not hit Return)

    cd Desk
Now hit the tab key. See what happens? It completes the work "Desk" to "Desktop", because it is the only thing in your home folder that starts with "Desk" (if you actually do have something else in your folder that starts with "Desk", this example will not work).

As another example type (but do not hit Return)

    cd D
and hit tab again. There are at least two things in your home directory that start with D: Desktop and Documents. OS X does not know which one to complete to, so it lists the possibilities for you. Tab autocompletion only works when the Terminal has enough information to uniquely pick one option from the current folder. Try doing this again with
    cd De
What happens?

Changing Multiple Directories at Once

Suppose you are currently in the your home directory; you want to move to the folder "iTunes" which is inside of "Music". You could do this with two cd commands. But to do it with a single command, you just connect the folders with a /, as follows:

    cd Music/iTunes

When you combine this with .., you can do some rather clever tricks. Suppose you are currently in the Desktop directory, and you want to move in the Documents directory (which is contained in your home directory). You can do this with the command

    cd ../Documents

We refer to these expressions as paths; they are are a "path" from the working directory to the directory that you want to go to.

Absolute Paths

The paths that we have shown you are more properly called relative paths. They show how to get from the working directory to your new directory. The correct path to use depends on exactly which directory is the current working directory.

Absolute paths are paths that do not depend on the working directory. In OS X (and all Unix systems), absolute paths start with a /. This / represents the root directory that contains everything else. For example, if you wanted to go to your Applications directory (which is just inside the root directory), you would type

    cd /Applications

Absolute paths are very important when you are trying to navigate to a different disk drive. In OS X, when you plug in a new disk drive it is added to the /Volumes folder (note the / indicating that Volumes is just inside the root folder). Suppose you have a Kingston USB drive from the Campus store named KINGSTON. To view the contents of this drive in the terminal, type

    cd /Volumes/KINGSTON

To drive home the difference between relative and absolute paths, create a folder called "Applications" in your home directory. Make sure the terminal is in the home directory (go home by typing cd ~) and type

    cd Applications
Look at the contents with ls. Now go back to the home directory again. Type
    cd /Applications
and look at the contents with ls. See the difference?

Folder Names with Spaces

The Terminal breaks up the commands that you type in by spaces. That means that if you have a folder with spaces in the name, it will break it up into references to two different folders. For example, suppose you have a folder called "Python Examples", and you type

    cd Python Examples
You will (likely) get an error saying that the folder "Python" does not exist.

To solve the problem, put the directory in quotes. The following should work correctly.

    cd "Python Examples"
If you are changing multiple directories then you need to put the entire path in quotes (not just the folder). For example, if "Python Examples" were on the Desktop, type
    cd "Desktop/Program Files"

Alternatively, you can represent a space using the escape character \ which we talked about in class. For example, the following should also work correctly:

    cd Python\ Examples
If you use Tab Completion a lot, you will notice that this is the prefered way of handling spaces.


Manipulating Files

The Terminal allows you to do everything that Finder can do (and more). Some of the things that you might find the Terminal for is making folders, moving files, and deleting files. However, it is important to understand that none of this is necessary. For this class, you never need to understand how to do anything other than navigate directories. You can do everything else in Finder (or some other program) if you wish.

Make a Directory

To make a new folder or directory, use the command mkdir followed by the name of the new folder. For example:

    mkdir MyFolder
The new folder will appear in the current working directory.

You can also delete a directory with the rmdir command. For example, to delete the folder we just made, type

    rmdir MyFolder
The Terminal will only delete empty directories. If there is anything in a directory, it will not let you delete it. You have to delete the contents first.

Move (or Rename) a File

You move files with the mv command. The way this command works is that you give it two file names. It searches for a file with the first file name; once it finds it, it makes a copy with the new file name and then deletes the original.

For example, suppose you wanted to rename the file test.py to assignment3.py. Then you would type

    mv test.py assignment3.py
(this by the way, illustrates why paths cannot have spaces in them).

If the second filename is path to a file, then it will move the the file into the correct directory. For example, suppose we now wanted to move assignment3.py to the Desktop (which is a folder in the current working directory), and rename it completed.py. Then we would type

   mv assignment3.py Desktop/completed.py
If we want to keep the name as assignment3.py, we could shorten this to
   mv assignment3.py Desktop
In this case, the Terminal will move assignment3.py into Desktop, but keep the name of the file unchanged.

Copy a File

The mv command will always delete the original (name of) the file when it is done. Sometimes we want to make a copy of a file. We do that with the cp command. Suppose that assignment3.py is in the working directory and we want to put a copy on the Desktop without deleting the original. Then we would type

   copy assignment3.py Desktop/assignment3.py

Delete a File

Files are deleted with the rm command. In our running example, to delete the file assignment3.py, you would type

    rm assignment3.py

Be very careful with this command. It completely erases the file. It does not move the file your Trash. You cannot retrieve a file deleted this way. If you have used our bashrc installer, then we have changed the settings of rm so that it will ask for confirmation before deleting. That way you will not delete anything by mistake.


Getting Help

There are many resources out there on how to learn to use the Terminal in OS X. If you want to learn more, we suggest this tutorial as a starting point.

If have are having difficulty with the Terminal, please see one of the course staff or consultants. They are available to help.


Linux

Let's be honest here. If you use Linux, do you really need to learn how to use the command shell? How is it possible to do anything in Linux without knowing how to use the command shell?

On the off chance that you honestly have never used a command shell in Linux, the hard part is finding the program that provides access to the shell. Which program to use depends on your choice of GUI.

Once you have that running, simply refer to the instructions for OS X. While the programs are not exactly the same (particularly if you are running a shell other than Bash), they are close enough for purposes of this class.

If you need more help with using the command shell in Linux, please see one of the course staff or consultants.