Unix Lab

The purpose of this lab is to teach you some basic Unix commands for those who are using a Mac OS X machine. Mac OS X is a version of Unix. If you are using a Windows machine, read the DOS Lab instead.


Introduction

You are probably already familiar with interacting with files and folders on your Mac through the "Finder" program. Finder is a GUI (Graphical User Interface) which allows you to navigate through your file system and create new folders and documents, and organize them in ways that make sense to you. Sometimes in programming however, you will need to use your file system in a different way. For this, on Mac, you will use a program called "Terminal". Terminal is known as a CLI (Command Line Interface). That means in addition to accessing your file system, you can also use Terminal to run Python programs directly, among many other programming tasks.

The purpose of this lab is to introduce you to some of the basic commands you will commonly use when interacting with Terminal. Many of these are very short abbreviations of longer words or phrases, so knowing what they stand for may help you remember and understand them better. If you are curious to explore the full list of Unix commands, you can find it here.

Also note that the file system on your computer is structured in a hierarchical way. That means that files and folders are nested inside of other folders. There is some specific terminology we will use when dealing with hierarchical structures like this: directory (also called folder), parent directory, sub-directory, and file. When one directory contains another directory, the containing one is called the "parent directory", and the contained one is called the "sub-directory."

Instructions

1. Launching Terminal

  1. Open a new Finder window by clicking the "Finder" icon on your dock
  2. Navigate to the "Applications" directory (usually shown on the left side of Finder, under "Favorites")
  3. Open the "Utilities" folder from within Applications
  4. Open the "Terminal" program

You should see a mostly-blank window open that shows your computer username, followed by the $ symbol. This is called the Terminal prompt, and you will often read or hear the phrase "enter this text at the prompt." That means type that text after the $.

Whenever you open a new Terminal window like this, you will be put into the "Home" directory. This is equivalent to opening a new Finder window and clicking on the entry in your "Favorites" that has the small house icon next to it.

Note: You may want to add the Terminal program to your Mac's Dock for easier access. To do this, drag the Terminal program from within the Applications/Utilities folder in Finder on to your Dock.

2. Entering Commands

An important thing to note is that in the command prompt you can use "tab-complete" to automatically finish the name of a file or directory so you don't have to type the whole thing. This will save you a tremendous amount of time and avoid typing mistakes. To tab-complete, begin typing the name of the file or directory and then hit the Tab key. If the command prompt is able to map what you've typed to a unique location, it will complete the name for you. If the command prompt finds multiple files or folders with a similar name, it will not autocomplete since it cannot figure out which file you mean, and you need to type more characters until it can get a specific match.

If at any point you want more information about a particular command you have entered, you can type

  man
followed by the name of the command you want information on. man stands for manual, and this command will display the instruction manual for that command, including a description of what it does and all the extra options you can add to change what the command does. To exit from viewing the manual page, type q (for quit). Now you try it yourself!

Enter the following at the terminal prompt:

  man pwd

The pwd command stands for print working directory, and it tells you where the terminal program is currently looking in within your file system, using the absolute path. You can always use this command to verify what folder or directory you are inside of.

3. Navigating

In the following exercises, you will try moving around various directories and creating and modifying files on your computer. As you follow the steps, try to find the same directories and files using the Finder program at the same time so you can see the effects of the commands you are entering.

The Lab

1. Create a new directory

  mkdir NewDirectory
mkdir stands for make directory, and it creates a new directory (or folder) named whatever you type after mkdir.

2. List all of the files and directories in your current location

  ls
ls stands for list. ls is often used with extra options to print out more information than just file names. For example, ls -l (a lowercase letter L, not a number 1) will print out all files and directories using their long form, which includes permissions, file size, time last edited, and other metadata about each file. You can use the man command to find out more about all the options available to use with each command, for example man ls.

3. Move in to "NewDirectory", the directory you just created

  cd NewDirectory/
The cd command stands for connect directory, and it moves you in Terminal to the directory you entered after cd. Note that NewDirectory has a / character at the end. This indicates that NewDirectory is a directory, rather than a file. When you type this you are moving down a level in the file hierarchy. If at any point you want to get back to your Home directory, no matter where you are in the file system, you can just type cd with nothing after it, and you will be returned to the Home directory.

4. Create a new blank file within NewDirectory

  touch HelloUnix.txt
By entering the .txt at the end of the filename, you are creating a new file that is a text file. If you entered a different extension, you would be creating a different type of file. For example, many image files end in .jpg to indicate they are JPEG files.

5. Comparing with Finder

All the changes you make in Terminal are reflected on your actual file system, which you can see in Finder. Now you are going to see that it works the other way too! Go back to your Finder window, navigate manually to NewDirectory and open the HelloUnix.txt file. It should open in the your default text editor (likely TextEdit). Type your name, your major, and your favorite color on separate lines and then save the file and close it using the "X". Then go back to the Terminal window.

6. View the contents of HelloUnix.txt

  more HelloUnix.txt
In addition to the more command, which shows you the contents of the file page by page, you can also view just the beginning of a file's contents using the head command, and just the end of a file's contents using the tail command. To exit from viewing the file's contents, type q.

7. Rename the new file you just created

  mv HelloUnix.txt GoodbyeUnix.txt
mv stands for move. But wait, aren't you supposed to rename the file, not move it? Well to your computer's file system, they are the same thing, because the "name" of something is actually the location on the computer where it is stored, so from the computer's perspective, renaming a file means moving it to a different spot.

8. Delete the file you just renamed

  rm -i GoodbyeUnix.txt
Here you use the -i option with rm so that you get a confirmation prompt before deleting the file, because once a file has been deleted, it is gone forever! If you are sure you want to delete something, you can omit the -i option and just enter rm <filename>, but be careful with that command!

9. Navigate out of NewDirectory, back to your Desktop

  cd ..
A single . indicates the directory you are in. If you typed cd . nothing would happen! But when you use two dots, that means go to the parent directory of wherever you are. It moves you one level up the hierarchy.

10. View a list of all the commands you just typed

  history
history will show you, in reverse order, all of the commands you have entered into Terminal so far.

If you are interested in learning more about Unix and the Terminal, Wikipedia's page on Unix and Apple's Command Line Documentation are a good place to start!

4. Some Java Related Commands

Read this section after you install your Java compiler.

These are some Java related commands that you can issue on a DOS command prompt. These commands will work only if you have installed a Java compiler with the PATH environment variable set appropriately:


  1. To compile a Java file:

     javac javaFileName

     (ex) javac Hello.java
     
	 
  2. To run/execute a Java program:  
  
     java javaClassfileNameWithoutTheDotClassExtension

     (ex) java Hello