Unix in 15 Minutes

There are only a few Unix commands that you will need to master to make it through COS 126. The following is a brief tutorial covering the most important ones.

 
What is Unix

Briefly, Unix is an operating system, which means it is a program that interacts with the hardware (such as processors, disks, keyboard, mouse) and manages their resources (such as keeping track of where a file is on a disk). The operating system you are most familiar is probably Windows. Just like Windows, Unix organizes the files you create and the application programs you will use. Unix works at a lower level than Windows. This means that you will have more control over the machine. The disadvantage is that it is less user-friendly.

Unix comes with thousands of commands, most of which you will not need to use in COS 126. After following this brief tutorial (about 15 minutes), you will be acquainted with enough Unix to get you through COS 126.

You enter Unix commands in a terminal window. The Unix prompt should look something like:

phoenix.Princeton.EDU%

This is where you type commands. The boldface type below (that follows the Unix prompt) is what you should type as you work through this tutorial.

Some Useful Commands

Working with Files and Directories

Unix organizes files into a directory hierarchy. A directory can contain both files and other directories. They are completely analogous to folders in Windows.

Redirection

Two important abstractions in Unix are standard input and standard output. By default standard input is your keyboard, and standard output is your monitor. For example, in Assignment 1, we write a program CenterofMass.java that reads input using StdIn.java and writes output using System.out.println(). To run our program, the user types the command "java CenterofMass" and enters double type values in triplets (xposition, yposition and mass) from the keyboard. The results appear in the terminal window.

phoenix.Princeton.EDU% java CenterofMass
0 0 10
1 1 10
0.5  0.5  20

 
Piping

Another useful abstraction in Unix is piping.  Piping is when the output of one program is used as the input of another program. For example, suppose we want to view the output of a program, but there is so much that it whizzes by on the screen too fast to read. (The program RandInts.java prints out a bunch of random integers.) One possible way to accomplish this is to type the following two commands.

phoenix.Princeton.EDU% java RandInts > temp.txt
phoenix.Princeton.EDU% more < temp.txt
Note that more will work by redirecting the file temp.txt to standard input (as is done here) or by simply using the filename (as is done at the beginning of the document). Instead, we could do this in one line using the pipe symbol '|'
phoenix.Princeton.EDU% java RandInts | more

This is often useful when debugging a program, especially if your program goes into an infinite loop and you want to see the first few values that it prints.


 
Written by Jake Brenner, Donna Gabai and Kevin Wayne.