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
phoenix.Princeton.EDU% javac HelloWorld.java
phoenix.Princeton.EDU% more HelloWorld.java
Unix organizes files into a directory hierarchy. A directory can contain both files and other directories. They are completely analogous to folders in Windows.
If you've just logged in, you should see your login name at the end of the directory name. This is your home directory.phoenix.Princeton.EDU% pwd /n/homeserver0/user0i/wayne
There are 6 items in this directory. Some of them are files, like HelloWorld.java. Others are directories, like public_html. If you want to know which of these items are files and which are directories, you can use the command ls -F. The -F is called a flag. Many Unix commands have such flags that give you precise control over their behavior, in this case appending the '/' character to items that are directories.phoenix.Princeton.EDU% ls Mail SunWS_config autosave HelloWorld.java public_html readme
phoenix.Princeton.EDU% ls -F Mail/ SunWS_config/ autosave/ HelloWorld.java public_html/ readme.txt
phoenix.Princeton.EDU% mkdir hello
To see that it actually worked, use the ls command.
phoenix.Princeton.EDU% ls -F Mail/ SunWS_config/ autosave/ hello/ hello.c public_html/ readme
The two files are no longer visible from the current directory.phoenix.Princeton.EDU% mv HelloWorld.java hello phoenix.Princeton.EDU% mv readme.txt hello phoenix.Princeton.EDU% ls -F Mail/ SunWS_config/ autosave/ hello/ public_html/
phoenix.Princeton.EDU% cd hello phoenix.Princeton.EDU% pwd /n/homeserver0/user0i/wayne/hello phoenix.Princeton.EDU% ls HelloWorld.java readme.txt
phoenix.Princeton.EDU% cp HelloWorld.java HelloWorld.bak phoenix.Princeton.EDU% ls HelloWorld.bak HelloWorld.java readme.txt
phoenix.Princeton.EDU% rm HelloWorld.bak rm: remove HelloWorld.bak (yes/no)? y phoenix.Princeton.EDU% ls HelloWorld.java readme.txt
To return back to your home directory, use the cd command, but this time with no directory name.
phoenix.Princeton.EDU% cd phoenix.Princeton.EDU% pwd /n/homeserver0/user0i/wayne
phoenix.Princeton.EDU% mkdir loops phoenix.Princeton.EDU% cp /u/cos126/files/loops/* loops
Here the * matches all files in the cos126/files/mandel directory. It copies them to your newly created mandel directory.
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
phoenix.Princeton.EDU% more input.txt 0 0 10 1 1 10
Then to read the integers from the file instead of the keyboard, we use the redirection symbol "<".
phoenix.Princeton.EDU% java CenterofMass < input.txt 0.5 0.5 20
This produces exactly the same result as if the user had typed the numbers, except that the user has no opportunity to enter numbers from the keyboard. This is especially useful for two reasons. First, if there are lots of input values (there are over 700 inputs for Assignment 2) it would be tedious to retype them in each time we run our program. Second, it allows programs to be automated, without waiting for user interaction. This means that your grader can process your homework programs without typing in the input values by hand each time.
The user still types in the input values from the keyboard, but instead of sending the output to the screen, it is sent to the file named output.txt. Note that all printf output is sent to the file, even the statement that tells the user what to do. Be careful, if the file output.txt already exists, it will be overwritten. (To append use '>>' instead.)phoenix.Princeton.EDU% java CenterofMass > output.txt 0 0 10 1 1 10
phoenix.Princeton.EDU% more output.txt
After executing this command, no output appears on the screen, but the file output2.txt now contains exactly the same data as output.txt above.phoenix.Princeton.EDU% java CenterofMass < input.txt > output2.txt
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.
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 > temp.txt phoenix.Princeton.EDU% more < temp.txt
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.