CSC 161 Grinnell College Fall, 2013
 
Imperative Problem Solving and Data Structures
 
 

Basic Linux Commands and Capabilities

Abstract

The original Unix operating system and its successor Linux were designed to support the common tasks and needs of computer users. This laboratory exercise reviews some basic commands and capabilities that match likely needs of CSC 161 students. The next lab and its successor will continue on more Linux basics.

Introduction

This lab covers the capabilities and commands for a terminal window in the following table.

Topic Category Subtopics Linux Commands
Terminal Window open click on Desktop menu item
change password password
copying between windows left mouse button to highlight/copy; middle mouse button to paste
terminal utilities sleep, history, arrow keys, cat
autocompletion TAB
background process & operator at end of command
manual man
close control d
Help manual man

Before progressing further in this lab, be sure you have completed the readings for this lab.

The Terminal Window

For most of this lab, you will be experimenting with a terminal window.

Opening the Terminal Window

  1. Open a terminal window.

    1. You may already have a terminal window on screen. If not, start one by moving the pointer onto the small monitor icon on the bottom row of the front panel, and click with the left mouse button. If you do not have the terminal icon, speak with the instructor.
    2. Move your mouse to the top border of the terminal window, click the left mouse. With the left mouse button depressed, move the terminal window to the upper left of your screen. (If you move the terminal window too far, it may go into another "workspace", and the system may display only material in this new "workspace". If this happens and you want all material together, just move the terminal window back in the opposite direction from before, so the terminal window will be put back into the old workspace.)

Changing Your Password

  1. Choose a new password. Make it something that you can easily remember, but not an English word or a name, since it is easy for system crackers to break in by guessing your password if you choose it from one of those categories.

    Use the password program within a terminal window to change your password.

Cutting and Pasting Between Windows

For most of this course, you will need to use a text editor that does not add behind-the-scenes formatting details (e.g., fonts, paragraph styles, heading styles, etc.). I suggest that you use emacs (e.g., vi is another option), since emacs is quite powerful, has capabilities useful for C programming, and provides buttons and other features that may seem similar to other environments.

emacs can be opened from the Xfce menu (with the picture of a mouse) at the bottom of screen (look under the "Development" section for "GNU Emacs". However, much of the work we will be doing all semester will be based in a terminal window, so it may be helpful to gain experience with a terminal environment now.

  1. Cut and paste material between windows.
    1. Open a terminal window.
    2. To create a new file "my-test-file", or to edit an existing file by this name, move the mouse to the terminal window and type
      emacs my-test-file &
      

      Note the ampersand & at the end of the line. We'll talk more about that later in this lab.

      Since emacs is an extremely powerful editor, stray keystrokes might be interpreted as being part of special commands. If emacs seems to behave in a peculiar way when you are editing, note that control g (Ctrl g) stops any command, so you can go back to normal editing.

    3. Consider the following Scheme procedure definition:

         (define mult5 
             (lambda (n)
                (* n 5)
             )
         )
      

      Select this material from this lab exercise in your browser as follows: move the cursor to the beginning of a section and push down the left mouse button. Then, holding the button down, move the mouse to the end of the section. (The entire section now should be highlighted.) When the desired section is highlighted, stop pressing on the left mouse button — the section should stay highlighted.

    4. Move the mouse to the emacs window, and click the middle mouse button to paste the definition into emacs.

      Note: You can cut and paste between windows without using the control Ctrl key. Linux does not require Ctrl c and Ctrl v — just use the left and middle mouse buttons.

    5. What happens if you click the left button twice within emacs?

    6. Can you paste this definition into both emacs and the terminal window?

    7. Before going on to the next part of this lab, save your emacs file. (It should be named my-test-file.

Terminal Utilities

This section asks you to practice with several useful commands within a terminal window.

Stopping a Long or Infinite Loop

  1. Within a terminal window, launch the sleep utility for 10 seconds:

       sleep 10
    

    Kill processing in the window with ctrl-c.

The Arrow Keys

  1. Move your mouse to a terminal window, and type several commands (e.g., use cat to display several Scheme programs you have saved during your previous CSC 151 course) and use the sleep command a few times. (This work creates a past record of work you have done in this terminal window.)

  2. Use the up-arrow key to retrieve several previous commands. Then, use the down-arrow to move to more recent commands.

  3. After you have retrieved the sleep 10 command, use the left-arrow key and then the delete key to edit the line to sleep 5. Then hit return and note what happens.

  4. Now use the up-arrow and left-arrow key to type a 1 before the 5 to produce sleep 15. (Do not use the right-arrow key to move the cursor to the end of the line.) Hit return, and use your watch to determine how long the sleep lasts. Note that you can edit any part of a line and then hit return to enter the entire line into the terminal window.

  5. Type history into the terminal window to get a list of recent commands you have issued in this terminal window.

  6. To re-issue a command, type an exclamation point followed by the command number from the history command. For example, suppose history gave you the list

      505  sleep 10
      506  history
    

    Then typing "!505" allows you to re-issue the sleep command.

    Now re-issue the history command in this way.

Background Processes

  1. Suppose you want to read the pdf file ~walker/c/examples/intro.pdf with the evince postscript/pdf reader.

    1. First, type

         evince ~walker/c/examples/intro.pdf
      

      This will open evince reader just fine, but now switch back to your terminal window. You will notice that it is unavailable for further use (i.e., you won't get another command prompt) until acrobat reader is closed.

    2. Now close evince reader, and then re-open it from the terminal window, but this time add an ampersand character to the end of the command:

         evince ~walker/c/examples/intro.pdf &
      
    3. Now when you return to your terminal window, a prompt is waiting for you, making it easy to do multiple tasks at once. Adding the ampersand character to a command causes the command to be launched as a "background process," allowing you to continue working with your terminal window.

Autocompletion

  1. Try using "autocompletion" with the file my-test-file by typing

    "cat ~/my-t" followed by the TAB key
    

    You should find that when you press TAB the system completes the (unique) file name for you. (The command name cat stands for "concatenate" for reasons we will see later. It can be used to display the contents of a text file, so you should see the file my-test-file scroll by when you use this command.)

  2. Create a new file my-test-stuff using the emacs editor.

    1. What if you press TAB too early, such that there isn't a unique completion? Try this by typing

      cat ~/my-t  followed by TAB
      
    2. Hit TAB a second time to see how the system responds.

    3. What happens if you type cat zqrz followed by TAB (where I am assuming you do NOT have a file zqrz)?

  3. Note that autocompletion also works with commands.

    1. Try typing his followed by TAB.

    2. What is the fewest number of characters you can type to produce the history command?

    3. What other commands begin with the letter h?

    4. Are there any commands that begin with her?

The Manual

  1. Use the man command to consult the online Linux manual (Note the q key is used to exit man pages):

    1. What do the following command options mean? Note the ability to smash flags together.

        cp -p
        ls -ltrF
        mkdir -p
      
    2. Why does the cat command have the name "cat", which stands for "concatenate"?

    3. Use the man page for the C function sqrt to identify the parameters and return type for a collection of functions related to sqrt. Also, what "include" statement is needed for sqrt

  2. The command man -k keyword lists commands that seem related to the given keyword. For example, to print a list of man pages that include the word "square" in the name or description fields, you could use "man -k square". Try this command to locate sqrt and to determine various commands related to the keyword "print".

Closing a Terminal Window

  1. Try closing a terminal window by typing Ctrl/d

Feedback Welcome

Development of laboratory exercises is an iterative process. Prof. Walker welcomes your feedback! Feel free to talk to him during class or stop by his office.