OS X / Linux / Unix Command-Line in 15 Minutes


This tutorial describes a few of the most important commands that you will use on the command-line (Linux, Unix, or OS X).


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.