This set of notes assumes that you have never used Unix before. It does assume, however, that you are comfortable using computers with a graphical user interface, such as the Macintosh operating system or Microsoft Windows. You should sit down at a Mac in the lab and go through this tutorial trying things out as they are explained. Upon completion, you should be able to understand the basics of entering a command line, move around in the file system and view existing files, do basic manipulations of windows, do basic editing with the Emacs text editor, running and know where to find help. To gain experience with these topics, try things out as you read.
All Mac Windows look like the following window:
Here are some basic window management operations:
You can start to get the feel for the environment by clicking on the Safari icon to start a web browser. The safari icon is in the tool bar and looks like the following:
Go to the class web page at http://www.cs.pomona.edu/classes/cs062/ and find the electronic version of this page (look for documentation). Bookmark this page -- you will be referring to it often during lab.
This will open a shell window in which will type Unix commands. For example, type the command "ls" in the shell window and press return. This command will list the files in your directory. A command consists of the name of a program, zero or more options preceded by '-', and zero or more arguments, which are themselves words. The command line is terminated with a carriage return.
If you make mistakes in typing on the command line, use the Delete or Backspace key to erase the previous character.
Here are other useful commands related to files:
One thing you will notice with Unix is that "no news is good news". Often if a Unix command is successful, it will produce no output! If it fails, you will receive an error message. Of the list of commands above, only "ls" and "more" produce output when they succeed.
ls List the files in the current directory cp file1 file2 Copy the file named file1 to a new file named file2 mv file1 file2 Rename the file named file1 to have the name file2. (mv is actually short for move, but it doesn't necessarily move anything!) rm file1 Remove the file named file1 more file1 Display the contents of a file lpr file1 Print the file named file1
The "more" command deserves some more comment because it is an interactive command. It displays your file one screenful at a time. After each screenful, it displays a prompt either consisting of the name of the file being displayed in reverse video (for the first screenful), (END) in reverse video (for the last screenful), or : (for all other screenfuls). The prompt appears at the bottom of the screen. At this prompt, you have several options, the most useful being:
You can try out "more" by typing "more /private/var/automount/common/cs/cs062/alice.txt". This will show you a file containing the first chapter of Alice in Wonderland. (One useful tidbit: hitting the tab key will autocomplete directory and file names. Try typing "/private/var/auto" and then hitting the tab key.)
space Display the next screenful b Display the previous screenful q Quit "more", returning to a shell prompt.
When you use "ls" to list your files, a directory will appear with / at the end of its name. At all times you have a current directory. Your commands are interpreted with respect to your current directory by default. So, for example, ls lists the files in your current directory if you give it no arguments. Here are some useful commands to manipulate directories:
Filenames should consist only of letters, numbers, and '.' A filename is typically divided into a descriptive name and an extension, separated by '.'. Extensions are purely by convention but typically indicate the type of file. For example, "myfile.txt" would be a text file, while "myfile.java" would contain a Java program. Extensions are generally optional although some programs, such as the Java compiler, expect them to be there.
mkdir dir1 Create a new directory named dir1. rmdir dir1 Remove the directory named dir1. You can only do this if the directory is empty. cd dir1 Make dir1 be the current directory pwd Display the name of the current working directory
Filenames can be given as names that are relative to the current working directory or as absolute names from the special root directory. Thus far, we have assumed relative names that refer simply to files in the current directory. We can also use relative names to identify files in subdirectories. For example, "mydir/myfile" is the file named "myfile" in the directory named "mydir", where "mydir" is located in the current working directory.
Each user also has a home directory This is the directory that is the current directory when you start a new shell window. (You have already found your home directory through the standard Mac interface). No matter what the absolute pathname is to your home directory, you can always refer to it with the special relative pathname '~'. For example, no matter what your current directory is, if you type "ls ~", you will see a listing of the files in your home directory.
An absolute pathname always begins with / while a relative pathname never does. To find out the pathname that corresponds to your current directory, type pwd. Use pwd to find the absolute path for your home directory. One last useful feature of the command line: Pressing the up arrow will give the last command you executed. Pressing return will run that command again. Try this. You can access older commands in your history by hitting the up arrow multiple times. To practice these commands, copy the alice.txt file to your home directory with the command
cp /private/var/automount/common/cs/cs062/alice.txt ~/alice.txt
Rename the file, move it into a newly created subdirectory, and practice the other basic Unix commands as well.
Close a terminal either by clicking on the red circle in the title bar or by typing the "exit" command at the prompt.
The top row consists of icons for common operations. Initially, we will work with a few of these icons, the menu across the top of the screen, and a few keyboard commands. Over the month, your repertoire of commands will grow and will move away from menus and toward keyboard commands. To see what the icons do, just hold the mouse over each and text will appear explaining what the icon does. For now, let's create a new file. To do this, point at the "File" menu and click the mouse button. Then select "New".
The last line of the emacs window is where command prompts and messages appear. The reverse video line immediately preceding it is a status line. It shows you the name of the file you are editing. Before that you should see -:--. This means that the file is up-to-date. If it says, -:**, it means the buffer has been changed since it was last saved. If it says ---%%, it means that you cannot modify the file.
After the filename is an indicator of where you are in the file using a percentage. "All" indicates that the entire file is showing. "Top" indicates that you are on the first screenful. "Bottom" indicates that you are on the last screenful. Otherwise it will say something like 21%.
After this is a pair of integers in parentheses, e.g. (40,37). The first number represents the line of the file the cursor is on, while the second number represents the character number in the line where the cursor is. After this is the "mode" that you are editing in. This should say "(Fundamental)", or "(Java)" when editing a Java file.
The area between the menu titles and the status line is the buffer that displays the contents of the file. Most of your typing goes directly into this buffer. Type some text. Notice the status line changes. You can move the cursor around using the arrow keys or the mouse. Use the scrollbar to move through the file. Use the "Save Buffer" command from the File menu to save the buffer to a file. Use "Quit Aquamacs Emacs" from the Aquamacs Emacs menu to exit the program. Notice the characters that appear in parentheses at the ends of command names in the menus. These tell you the keyboard characters to type to execute the commands without using the commands. Unfortunately these will only work on a Macintosh. There are other commands that will work for emacs running on any platform. For example, saving the buffer can be executed by typing Control-x Control-s as well as the "fan"-S shown in the menu. In the emacs documenation control characters appear as C-. Type these by holding the control key down at the same time as typing the second character. Another common way to enter commands is using the escape key. This is abbreviated M-. In this case the escape key and the following key are typed separately. For example M-x represents typing the Escape key followed by the "x" key. You can also press the "option" key and "x" at the same time to achieve the same effect. Here are the keyboard sequences for the most common Emacs commands:
C-v Next screenful M-v Previous screenful C-d Delete next character Backspace Delete previous character C-x C-s Save buffer to a file C-x C-f Open a file C-x C-c Exit emacs
You can find more emacs commands in the Emacs Hints Page.
Using keyboard commands is generally faster than moving one's hands back and forth between the mouse and keyboard as one must do to use the menus and scrollbar. While it takes extra effort to learn, the payoff is worth it in the long run in terms of speed of using the editor.
javac Hello.java
Assuming the program is free of compilation errors, a class file "Hello.class" will be created. This files contains the compiled representation of the program. To execute the program, you run the Java virtual machine with the command
java Hello
The command line argument to java is the name of the class whose main method you would like to run. Copy the file /private/var/automount/common/cs/cs062/Hello.java to your home directory, compile it, and run it.