Unix in 15 Minutes

There are only a few Unix commands that you will need to master to make it through COS 126. The following is a brief tutorial covering the most important ones.

 
What is Unix

Briefly, Unix is an operating system, which means it is a program that interacts with the hardware (such as processors, disks, keyboard, mouse) and manages their resources (such as keeping track of where a file is on a disk). The operating system you are most familiar is probably Microsoft DOS, Windows, or Mac OS. Just like these other operating systems, Unix organizes the files you create and the application programs you will use. Unix works at a lower level than Windows. This means that you will have more control over the machine. The disadvantage is that it is less user-friendly. Unix comes with thousands of commands, most of which you will not need to use in COS 126. After following this brief tutorial (about 15 minutes), you will be acquainted with enough Unix to get you through COS 126.

You enter Unix commands in a terminal window. The Unix prompt should look something like:

phoenix.Princeton.EDU%

This is where you type commands. The boldface type below (that follows the Unix prompt) is what you should type as you work through this tutorial.

Some Useful Commands

The printing and PostScript viewing commands below assume that you are working in the 101 lab.

  • gcc126:  To compile a C program, we recommend using the gcc126 command. It compiles the program using the gcc compiler, but turns on additional warnings that can be extremely helpful to novices (and veterans). Your program should compile without any errors or warnings (or if there are warnings be absolutely sure that they do not indicate a flaw in your program).  Ultimately, we will compile the assignments you turn in with the same command, so it is to your advantage to use it.
    phoenix.Princeton.EDU% gcc126 hello.c
  • enscript126:  To print a C program in the 101 lab, we recommend using the enscript126 command. It prints in landscape mode, with two column per page, and a nice header. This is the same command we use to print your assignments.
    phoenix.Princeton.EDU% enscript126 hello.c
  • more:  Display the contents of a file one screenful at a time.
    phoenix.Princeton.EDU% more mand4.ps
  • help126:  A short summary of the most frequently used commands.
    phoenix.Princeton.EDU% help126
    [command names and brief descriptions get printed to the screen]
  • gs:  To view a PostScript program from the 101 lab use the command gs.
    phoenix.Princeton.EDU% gs ~cs126/files/unix/mand.ps &
    
    If the file mand.ps were in your current working directory, you wouldn't type the directory ~cs126/files/unix/.

  • lpr126:  To print a PostScript program in the 101 lab, use the command lpr126. By default, CIT does not provide you with a default printer. Our version of the command does the job. Be absolutely sure that you are trying to print a legal PostScript program (view with gs first); otherwise you may needlessly waste a lot of paper.
    phoenix.Princeton.EDU% lpr126 ~cs126/files/unix/mand.ps
    
  • 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.

  • pwd:   It is frequently useful to know in which directory you are currently working. In order to find out, type pwd at the Unix prompt.
    phoenix.Princeton.EDU% pwd
    /n/homeserver0/user0i/wayne
    If you've just logged in, you should see your login name at the end of the directory name. This is your home directory.

     

  • ls:  To view the contents of a directory, type ls. This command will list all the files and directories within the current directory. It is analogous to clicking on a Windows folder to see what's inside.
    phoenix.Princeton.EDU% ls
    Mail          SunWS_config  autosave      hello.c       public_html   readme
    There are 6 items in this directory. Some of them are files, like hello.c. 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 -F
    Mail/          SunWS_config/  autosave/      hello.c        public_html/   readme

  • mkdir: To create a new directory, use the command mkdir. The following command creates a directory named hello, which you can use to to store all of your files associated with the Hello World assignment.
    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

  • mv: Now, move the two files hello.c and readme into the hello directory using the mv command.
    phoenix.Princeton.EDU% mv hello.c hello
    phoenix.Princeton.EDU% mv readme hello
    phoenix.Princeton.EDU% ls -F
    Mail/          SunWS_config/  autosave/      hello/         public_html/
    The two files are no longer visible from the current directory.

  • cd: To access the two files, change directories with the cd command. To confirm the directory change, you could use the pwd and ls commands.
    phoenix.Princeton.EDU% cd hello
    phoenix.Princeton.EDU% pwd
    /n/homeserver0/user0i/wayne/hello
    phoenix.Princeton.EDU% ls
    hello.c  readme

  • cp:  To make a copy of a file, use the cp command. The following command creates a backup copy of our hello.c program. This is especially useful when you modify a working program, but might want to revert back to the original version if your modifications don't succeed. 
    phoenix.Princeton.EDU% cp hello.c hello.bak
    phoenix.Princeton.EDU% ls 
    hello.bak  hello.c    readme

  • rm: Subsequently, you might want to clean up useless files. The rm command deletes a file. By default, you will have to confirm that you really want to delete a file by typing yes or y. The reason for this is that, unlike with Windows, deleted files are really deleted when you delete them. There is no Recycle Bin to act as a safety net. So be careful.  Files on arizona are backed up daily, so it may be possible to recover an essential file that you accidentally delete, but you will have to contact CIT to do so.
    phoenix.Princeton.EDU% rm hello.bak
    rm: remove hello.bak (yes/no)? y
    phoenix.Princeton.EDU% ls 
    hello.c  readme

    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
  • wildcards:  You can also apply the cp, rm, and mv commands to several files (or directories) at once. To create a new directory called mandel, and copy all of the files in the directory /u/cs126/files/mandel/ into this newly created directory type:
    phoenix.Princeton.EDU% mkdir mandel
    phoenix.Princeton.EDU% cp /u/cs126/files/mandel/* mandel

    Here the * matches all files in the cs126/files/mandel directory. It copies them to your newly created mandel directory.

  • 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 wrote a program that read input using scanf and wrote output using printf. To run our program, the user types the command "trace126" and enters four integers from the keyboard. The results appear in the terminal window.

    phoenix.Princeton.EDU% trace126
    Enter the parameters a, b, c, and M in that order.
    11 37 1 100
    
       1  48  65  52   9  36  33   0  37  44  21  68  85  72  29  56  53  20  57  64
      41  88   5  92  49  76  73  40  77  84  61   8  25  12  69  96  93  60  97   4
      81  28  45  32  89  16  13  80  17  24   1  48  65  52   9  36  33   0  37  44
      21  68  85  72  29  56  53  20  57  64  41  88   5  92  49  76  73  40  77  84
      61   8  25  12  69  96  93  60  97   4  81  28  45  32  89  16  13  80  17  24
  • Redirecting standard input. As an alternative, we could have created a file that consists of the same four integers. Using emacs, create a file named input.txt, and type type in the four integers. After saving the file, type the following command to verify that you entered the integers correctly:
    phoenix.Princeton.EDU% more input.txt
    11 37 1 100

    Then to read the integers from the file instead of the keyboard, we use the redirection symbol "<".

    phoenix.Princeton.EDU% trace126 < input.txt
    Enter the parameters a, b, c, and M in that order.
    
       1  48  65  52   9  36  33   0  37  44  21  68  85  72  29  56  53  20  57  64
      41  88   5  92  49  76  73  40  77  84  61   8  25  12  69  96  93  60  97   4
      81  28  45  32  89  16  13  80  17  24   1  48  65  52   9  36  33   0  37  44
      21  68  85  72  29  56  53  20  57  64  41  88   5  92  49  76  73  40  77  84
      61   8  25  12  69  96  93  60  97   4  81  28  45  32  89  16  13  80  17  24

    This produces exactly the same result as if the user had typed the integers, 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.

  • Redirecting standard output. Similarly it is possible to redirect the output to a file instead of to the screen. Continuing with the same example, if we want to save the output permanently, we can use the output redirection symbol '>'. 
    phoenix.Princeton.EDU% trace126 > output.txt
    11 37 1 100
    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% more output.txt
    Enter the parameters a, b, c, and M in that order.
    
       1  48  65  52   9  36  33   0  37  44  21  68  85  72  29  56  53  20  57  64
      41  88   5  92  49  76  73  40  77  84  61   8  25  12  69  96  93  60  97   4
      81  28  45  32  89  16  13  80  17  24   1  48  65  52   9  36  33   0  37  44
      21  68  85  72  29  56  53  20  57  64  41  88   5  92  49  76  73  40  77  84
      61   8  25  12  69  96  93  60  97   4  81  28  45  32  89  16  13  80  17  24
  • Redirecting standard input and standard output. It is often useful to use both redirection operations simultaneously.
    phoenix.Princeton.EDU% trace126 < input.txt > 
    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.
  •  
    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 prints out a bunch of random integers.) One possible way to accomplish this is to type the following two commands.
    phoenix.Princeton.EDU% 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% 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.

  • As a second example, suppose we want to print out the output of running the command "help126". We want to use the output of the help126 command as the input to a printing command. Piping gets the job done.
    phoenix.Princeton.EDU% help126 | lpr126
  • Writing COS 126 Programs With Emacs

    A C Program is a text file (similar to an .html file) that can be created using any word processor. Emacs is a powerful word processor specifically designed for writing code. It offers many features not found in Word or Notepad, including auto-indenting, auto-compiling, and syntax highlighting.

     

     


     
    Written by Jake Brenner and Kevin Wayne.
    Copyright © 2000, Robert Sedgewick