CS-211 Fall 2015 Lab 1 Introduction to UNIX

The Lab Environment

The lab at LNG-103 is a room with UNIX computers available to each student. You can log on to one of these machines using your normal PODS userid and password. When you do so, you can open a web browser and navigate to this page (www.cs.binghamton.edu/~tbarten1/CS211_Fall_2015/labs/Lab_01.html) to work on the lab.

When you are done, please log off the machine. To log off, find the person icon in the lower right hand corner of the screen. If you left click on that icon, it will bring up a pop-up menu that has, as one option, log out.

Introduction to Linux

Linux is a UNIX based operating system, widely used in the industry, and often preferred by professionals to more user oriented operating systems. For the last few decades, most engineering appliation software, especially Computer Aided Design (CAD) software has been written and used in a UNIX environment.

Unix supports a complete Graphical User Interface (you are using that now), with all of the tools like those you are used to using in Microsoft Windows or Apple OS X, such as web browsers, text editors, and file/directory browsers. However, the full graphic user interface tools are not as standardized on UNIX as they are in other places, and UNIX started out as a command line user interface. Therefore, it is worthwhile to get a basic understanding of the UNIX command line interface. To start this process, open a terminal window to enable command line processing.

Open a Terminal Window

In the lower left corner of the Graphical User Interface (GUI), there is a Menu button, similar to the Windows start button. Click on that button to bring up the list of programs, and choose Accessories. This will add sub-menu to your display the list all the accessory programs installed on this computer. Scroll down in the Accessories list and click on the icon labeled Terminal. (Note... avoid the icon labeled "Root Terminal" for now. That one opens a terminal window too, but has some minor differences that may be confusing at this point.) When you click on the terminal icon, a terminal window will open up on your desktop.

A terminal window is surrounded by a border. Within the border is a window title. The window title starts off as Terminal. The terminal window has its own menu line, with labels like File Edit View Search Terminal Help. Feel free to play around with the menu items a little. For instance, can you change the window title to Hello World?

The main body of the terminal window is a scrollable window that runs a UNIX command line. The command line in the UNIX window consists of things that you type, represented here by this font, and things the computer types back to you, represented here by this font.

Command Line Prompts

The computer has typed a "prompt" string in your terminal window to indicate that it is waiting for you to type something. This prompt string starts with the name of the machine you are running on followed by a colon. All of the machines in the lab are named linuxlabxx, where xx is a number. For instance, you may see linuxlab08:.

The second part of the prompt string is the "current directory". More about directories and the current directory shortly, but for now, your current directory is represented by a single tilde character, "~".

The last part of the prompt is a greater than sign, ">", to indicate that the terminal window is waiting for you to type a UNIX command. When you type a command and hit the enter key, that command is passed to the command interpreter. The command interpreter executes the command and possibly types its results back to the terminal window. When it is done, control returns to the terminal window, which prints a prompt, and then waits for your next command. So the entire prompt string will look something like :

   linuxlab08:~>

UNIX Files and Directories

Note - all the commands described here (plus a few more) are summarized here.

In UNIX, the file system is organized into files and directories, similar to Mac/OS and Windows. (Actually. Mac/OS and Windows copied the directory concept from UNIX.) Each directory is a container which can contain other directories, called sub-directories, or files. The top level directory is called "/", and forward slashes are used to delineate a directory from a sub-directory. Therefore, directory "/import/linux/home/tbarten1/Lab01" is a directory which is the Lab01 subdirectory of the "/import/linux/home/tbarten1" directory, which in turn is the "tbarten1" sub-directory of the "/import/linux/home" directory; and so on. We also say that a directory with a sub-directory is the "parent" directory of the sub-directory. For instance, "/import/linux/home" is the parent directory of the "/import/linux/home/tbarten1" sub-directory.

There are two special directories that UNIX keeps track of. The first is the "current directory", sometimes also called the "working directory". When you type a command in UNIX that needs to know what directory you are talking about, and you don't specify a directory, the "current directory" is the directory that will be used as a default. The "pwd" command stands for "Print Working Directory". At the command prompt in your terminal window, type pwd to see your current directory.

When commands need a file or a directory name, and you type a file or directory name that does not start with a forward slash (/), then UNIX pre-pends the file or directory name you specify with the current directory.

To change your current directory, use the "cd" (Change Directory) command, where the argument to the command is the directory that you want to become the current directory. UNIX uses the short-hand ".." to indicate "parent of the current directory". Type the command cd ... Notice how the prompt changes. Then type pwd to find out the current directory. Then type the command to change your current directory back to the directory you came from.

The second important directory for UNIX is your "home" directory. Your home directory is the directory that is your current directory when you first log in to a UNIX machine. Typically, your home directory is the starting place for your own personal disk space in UNIX. You have the authority to create files and sub-directories from your home directory, but others usually cannot read or write files in your home directory. The "cd" command with no arguments makes your home directory the current directory. In UNIX, there is a shorthand for your home directory - a single tilde. Thus "cd ~" and "cd" do the same thing. Notice that the tilde shorthand notation is used in the command prompt.

To list the contents of a directory, use the "ls" command, specifying the name of the directory as an argument. To list the contents of the current directory (which should now be your home directory", type ls. To list the contents of the top level directory, type ls /.

Commands can have special options to control how they operate. In UNIX, the convention is to indicate parameters which change how a command operates through the use of "flags". A flag starts with a dash, "-", followed usually by a single letter (but sometimes a full word.) For instance, the -l flag to the ls command tells ls to list more information about each file or subdirectory. Try typing ls -l to get a "long" list of what is in your home directory. You will see one line for each entry. The beginning of each line has a bunch of characters... rwxd, etc. If the very first character is a "d", the line refers to a sub-directory. If the first character is a dash, "-", the line refers to a file. Do you have any files in your home directory?

Managing Files and Directories

With your home directory as current directory, type mkdir junk. Then try an ls -l. Notice that your have created a new sub-directory of your home directory called "junk". Now, type ls -l junk. Notice that the "junk" sub-directory is empty. Now type cd junk to make the junk sub-directory your current diretory.

Type gedit test.txt. This opens a "gedit" editor widow on your desktop, and you are currently editing file "test.txt" in directory "~/junk". Look back at your terminal window, and notice that there is no prompt. The "gedit" command has not finished yet, so you are not allowed to type any new commmands on your command window. Exit out of the "gedit" editor window, and notice that the command line prompt appears after the "gedit" window closed. Normally, the UNIX command line processor runs on only one command at a time - but when you have an editor window open, it would be nice to be able to edit and run commmands at the same time. To fix this problem, type gedit test.txt&. In UNIX, an ampersand (&) at the end of a command tells UNIX to start the command in a new process, but as soon as the commmand is started, don't wait for it to stop... keep doing what you were doing. In this case, that means print the command prompt, and wait for the next command. Check your terminal window, and notice that there is a new prompt. In UNIX, when a command ends with an ampersand, we say it is "running in the background".

In the terminal window, do an ls -l. Notice that the directory is still empty, even though the editor is working on "test.txt". In the gedit window, do a Save. Then go back to the terminal window and do another ls -l. Notice that the test.txt file has been created in the junk subdirectory. Close the gedit window, and look at the terminal window again. Notice that when a background command ends, a message appears on the terminal window.

Now, do a cd to get back to your home directory, and type rmdir junk. What happens? Next, type rm junk/test.txt. Then do ls -l junk. Try removing the junk subdirectory again. Did it work this time?

If you would like to learn more about UNIX, there is a good tutorial here

Lab Report

Download and edit the following file: lab1_report.txt. Then submit your editted file on Blackboard in the Lab 1 submission area under the main CS-211 class page (not the lab section page) under "contents" in the Lab_01 report area.