CSE 214 - Unix Tutorial
By completing this Tutorial, students should learn the following:
- How to use SSH to login to a Sparky account
- Basic Unix commands for navigating and creating directories and files
- How to protect files from other users
- How to edit, compile, and execute Java programs in a Unix environment
- How to upload files to your Sparky account from your PC
What is SSH?
SSH (Secure Shell) is a program that can securely connect to Sparky, a Unix-based server where each student has ones own account. SSH is installed in most campus labs, including the SINC sites. In addition, students may download and install SSH on their own computers via Softweb. A secure connection is one where all communications between client and server are encrypted, and thus not accessible to packet sniffers and other snooping mechanisms.
Steps to Follow:
- Login to Sparky: Make sure you always use a secure program like SSH for accessing your Sparky account. Once you start SSH, click on the Quick Connect button, which should open a dialog with the login parameters. An example of the login dialog box is shown below.
Sparky's host name is sparky.ic.sunysb.edu and you must use Port Number 22 for secure communications. Note that your User Name is the name of your Sparky account (NetID) and home directory. It is most likely the first initial of your first name followed by your last name. So, John Smith might be "jsmith". Anyway, you should know this already since it is the same as your NetID, which you will also use to login to the class Blackboard page. For the Authentication Method, use Profile Settings. Use your NetID password to login.
When using SSH, you are actually making use of two operating systems. You have your personal computer's operating system (Windows or otherwise), and you have Unix on the Sparky server. Understand that when using SSH, Windows is simply providing the interface, a view of what is happening on Sparky. Your PC is collecting text you type and sending it to Sparky, then collecting the responses from Sparky and displaying the output in the SSH terminal window. Sparky is a server that runs a Unix operating system and it is doing the heavy lifting, like saving, compiling, and running your Java programs. It also handles messages sent to and from the PC where you are running SSH.
- Select a Unix Shell: A Unix Shell is the interface between you and the Unix operating system. By default, Sparky uses the C shell, which has limited advantages. A shell that will remember all commands you type is called the Bourne Shell (bash shell). To use the bash shell (it will make your life easier), from the command line simply type:
sparky% bash
Now that you are in the bash shell, your command prompt will say "bash-2.05$" rather than "sparky%". The advantage of usingbashis that by pressing the UP and DOWN-ARROW buttons, you may retrieve past entered commands. This comes in handy during debugging because you will be repeatedly using the same commands, and so will not have to re-type them each time.
Shells supported on Sparky include those below. Try them out and see which ones you like:
- default (
csh) - C shell command interpreter with a C-like syntax -
bash- GNU Bourne - Again SHell -
ksh- KornShell -
sh&jsh- standard and job control shell and command interpreter
- default (
- Where are we? When you login to Sparky, you start from your home directory. At any time, you may type
(present working directory) to see the name of the directory you are in. Try typingpwdand see what the path to your home directory is. In a Unix environment, thepwdcharacter is often used to refer to your home directory, so when you see it, that's what it is referring to.~
- Make your cse214 directories: You will want to create subdirectories in your account to place your homework files. Start by typing the following:
bash-2.05$ mkdir cse214
This will make the cse214 directory inside your present working directory, which since you just logged in, is your home directory. If you were to move to another directory and issue the same command, it would make a new directory there.
- List Directory Contents: Let's now verify that your make directory command worked properly. Type "
ls", which is short for list. This should list all of the files and folders inside your present working directory. You should see the cse214 directory that you just created.
- Build a skeleton of your directory structure: Store all your work for this class inside your cse214 directory and subdirectories. Organization is important when you with many different files. The directory in which you are working should only contain the necessary files for your current program. Now go into your cse214 subdirectory, and add hw directories, like hw1, which we will use for this exercise. Note that you should build a similar directory structure for your other hw directories.
In order to do this we will need to use thecdcommand, which is short for change directory. This command changes your present working directory, allowing you to navigate through your account's file system. Enter the following command sequence:
bash-2.05$ cd cse214
bash-2.05$ mkdir hws
bash-2.05$ cd hws
bash-2.05$ mkdir hw1
bash-2.05$ cd hw1
bash-2.05$ pwd
/.../loginname/cse214/hws/hw1
bash-2.05$
Note that in results from thepwdcommand above,loginnamewould be whatever your account's login name is.
- Open another terminal window: A Unix environment is very different from something like Windows. One obvious difference is that it is command-based, meaning you give typed commands to the computer rather than pointing and clicking a mouse. Thus, in Unix, each SSH window will only have one active program view at a time. So, when programming in Unix it is useful to have two (or more) SSH windows open simultaneously. One window will be for compiling and running (testing) the code that you write. We won't use it to open up pico, but rather, keep it at the bash command prompt in the directory where you are editing source files. Additional SSH windows would be for editing individual source files.
Open a new SSH window now by using your mouse to click on the New Terminal Window button. Then, turn on bash in that window and navigate yourself to the hw1 directory where you are going to do work in this tutorial. If you can not remember how to do so, press the UP key so that bash can scroll through commands you've already typed.
- Create a "
.java" file: Now you're ready to write and edit code. For today, we will do this using a text editor called "pico". Pico is a very simple text editor that one could use to edit Java source code. The pico program starts simply by typing "pico". We can also specify the file to edit when opening the program. Create a new Java source file calledDistanceCalculator.javausing pico as follows:
bash-2.05$ pico DistanceCalculator.java
- Define
DistanceCalculator.java: To start with, copy and paste the following code into yourDistanceCalculator.javafile.
import java.util.Scanner; import java.text.NumberFormat; /** * DistanceCalculator is a program that can be used to convert * between feet and miles using the English system of units. It * simply asks the user for input, makes the necessary conversions, * and then terminates. * * @author Richard McKenna */ public class DistanceCalculator { // THIS IS A CONSTANT TO BE USED IN OUR CONVERSION public static final int FEET_IN_A_MILE = 5280; /** * The main method is where the program starts its execution. It * will be the only method in this simple program. */ public static void main(String[] args) { // This Scanner object will help us retrieve user // input from the keyboard Scanner keyboard = new Scanner(System.in); // Prompt the user System.out.print("Enter the number of miles to convert: "); // Get the user input String userInput = keyboard.nextLine(); // Convert the input from text to a number double miles = Double.parseDouble(userInput); // Calculate # of feet double feet = miles * FEET_IN_A_MILE; // This NumberFormat object will format our numeric // output, putting the appropriate commas and abbreviating // our numbers to one decimal place NumberFormat nf = NumberFormat.getInstance(); nf.setMaximumFractionDigits(1); nf.setMinimumFractionDigits(1); String formattedFeet = nf.format(feet); String formattedMiles = nf.format(miles); // Display the output to the user System.out.println("There are " + formattedFeet + " feet in " + formattedMiles + " miles"); // And a little commentary System.out.println("Isn't the English System awful?"); } }
- Save the File: Now that you've made changes to the file, you will want to save your changes. If you look at the Pico interface, at the bottom of the screen there are a number of highlighted commands that start with the caret character, ^. These options may be used by pressing the Ctrl key while pressing the letter option. One of the options you should see there is "^O WriteOut". This option lets you save your work to the file. Let's do this, so type Ctrl-o. Then hit Enter to approve the command.
- Set your file permissions: Every file and folder in your account has a value that represents who is permitted to read, write, and execute that file or folder. You may use the
chmodcommand to set the permissions for a file, directory, or complete directory hierarchy. To make sure no one else can read (and thus steal) your work, go to the home directory, and use a recursive (-R) case of thechmodcommand to set all your directories and subdirectories. Then go back to yourhw1directory:
bash-2.05$ cd ~
bash-2.05$ chmod -R 700 *
bash-2.05$ cd cse214/hws/hw1
Note that*refers to everything. So you are saying set everything, including all files, directories, and subdirectories to this security setting. Alternatively, you could have done the same thing by simply using "chmod -R 700 ~/*". You may view the file permission settings on your files and folder by using this list command's long option. Try using the following:
bash-2.05$ ls -l
This should give you output similar, but not identical, to the following:
-rwx------ 1 loginname 39686 876 Sep 9 11:20 DistanceCalculator.java
You will notice that the permissions on this file are a dash '-', followed by 9 characters. These nine characters represent read (r), write (w), and execute (x) permissions for you (the first 3 characters), members of any Unix groups you are in (the next 3 characters), and all other Sparky users (the last 3 characters).rwx------means that you have full permissions for your files, and that no one else has any permissions to view, change or access them in any way. That is what you want for all of your files.
If you are wondering howchmodsets these permission values, you'll notice that we gave this command a three digit number. This number translates into rwx values using binary. The first number is permissions for you, the second is for groups, the third is for all numbers. 7 in binary is 111, so you are saying turn read on, write on, and execute on for yourself. 0 in binary is 000, so you are saying turn all access off for all groups and everyone else.
- Compile your program: Keep your current terminal window in Pico and switch to the other SSH terminal. You'll use it for all program compiling and running. Compile your source code (
DistanceCalculator.java) using thejavaccompiler using the following command:
bash-2.05$ javac DistanceCalculator.java
If it compiles properly it will create a file called DistanceCalculator.class, which is compiled Java code, ready to run on the Java Virtual Machine (JVM). If you get an error message, it's likely you made a copy and paste mistake. Remember, computers require precise instructions, so if it is missing the last bracket, for example, it will not compile. If you do not get an error message, typelsto make sure you have a newly compiled DistanceCalculator.class file.
- Run your Program: Once you have properly compiled your program, you can run it. Only those classes that have
mainmethods can be executed. To run your program you will have to start the Java Virtual Machine using thejavacommand, so type:
bash-2.05$ java DistanceCalculator
Running this program should produce the following results if the user enters 9999 miles:
Enter the number of miles to convert: 9999
There are 52,794,720.0 feet in 9,999.0 miles
Isn't the English System awful?
- Uploading Files to Sparky: In this tutorial we created a java source file using the Pico program from within the Unix environment. In the HW assignments, however, you'll be using eclipse to write your code on a PC. We can use SSH to transfer files from your PC to Sparky. To do so, press the New File Transfer Window button. This will open an FTP window that lets you transfer files between your PC and your Sparky account. Once your FTP window is open, your PC's file system will be on the left, and your Sparky file system will be on the right. On the left, navigate to the source files you wish to upload. On the right, navigate to the hw1 directory. Then drag the file from your PC to Sparky.
SUMMARY OF USEFUL UNIX COMMANDS
| Unix Command | Function | Example Usage |
.
|
Refers to the present working directory. |
See the cp example
|
..
|
Refers to the parent directory of the present working directory. |
See the cp example
|
~
|
Refers to the home directory of your account. |
See the ls example
|
*
|
A wildcard reference, it means everything in a directory. |
See the chmod example
|
cd
|
Traverse through existing directories by changing the present working directory. |
Change to the hw1 directory:cd hw1
|
chmod 700
|
Sets the file permissions for a file or files (first argument) such that no one but the account owner may read, write or, execute the given file(s). | Sets proper permissions for all .java files in the hw5 directory:chmod 700 hw5/*.java
|
chmod -R 700
|
Sets the file permissions for a directory or directories and its contents (first argument) such that no one but the account owner may read, write or, execute (traverse through) the given directory or directories. |
Sets proper permissions for the complete contents of the given account:chmod -R 700 ~/*
|
cp
|
Copies a file or files (first argument) from one location to another (second argument). |
To copy Hello.java from the parent directory into the current directory:cp ../Hello.java .
|
cp -R
|
Copies a directory or directories (first argument) from one location to another (second argument). |
To copy the hw1 directory and all its contents into the parent directory:cp -R hw1 ..
|
javac
|
To compile a Java program. |
To compile all .java files in the present working directoryjavac *.java
|
java
|
To run a compiled Java program. |
After compiling Test.java and all other classes needed to run the Test program (Test would need a main method):java Test
|
ls
|
Lists the contents of a directory (first and only argument). |
To list the contents of your home directory:ls ~
|
ls -l
|
Lists the contents of a directory (first and only argument) in a long output format, which gives complete information on all contents. |
To list the contents of your home directory in long form:ls -l ~
|
man
|
The Unix manual, it opens a help screen on a command topic (first and only argument). |
To get info on how to use the cp command:man cp
|
mkdir
|
Makes a directory using the name(s) of the argument(s). |
To create a directory named hw99:mkdir hw99 |
mv
|
Moves a file or files (first argument) from one location to another (second argument). |
To move all .java files from the present working directory to the home directory:mv *.java ~ |
mv -R
|
Moves a directory or directories (first argument) from one location to another (second argument). |
To move all directories starting with the letters "hw" files from the present working directory to the home directory:mv -R hw* ~
|
passwd
|
Allows the user to change his/her login password. |
passwd
|
pwd
|
Displays the full path to the present working directory. |
pwd
|
quota -v
|
Displays the disk quota and usage for the user's account. |
quota -v
|
rm
|
Removes a file or files (first argument) from one location to another (second argument). WARNING: Be careful using rm, it deletes files!
|
Remove all .class files from the present working directory:rm *.class
|
rm -R
|
Removes a directory or directories (first argument) from one location to another (second argument). WARNING: Be extremely careful using rm -R, it deletes entire directories!
|
Delete your test directory and all its contents:rm -R test
|
Web page created and maintained
by Richard McKenna