CSC 153 Grinnell College Spring, 2009
 
Computer Science Fundamentals
 

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 beginning CSC 153 students.

Introduction

This lab covers the following basic capabilities and commands.

Topic Category Subtopics Linux Commands
Terminal Window open  
change password password
copying between windows  
terminal utilities sleep, history, arrow keys, cat
background process  
autocompletion  
close  
Directories and Files paths ., .., ~, /, pwd
pathnames absolute, relative, pwd, cd, ls, which, whereis
file utilities mkdir, rmdir, rm, cp, mv, more, head, tail, pushd, popd
Help manual man
Printing printing lpr, a2ps, lpq, lprm
Permissions user, group, world ls -l
setting permissions chmod
start up .bashrc, .bash_profile, umask, alias

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. 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.

  2. Set the window aside for the moment (e.g., minimize the window) by clicking on the small bar or underscore character at the upper right-hand corner of the window, and restore the window by moving the pointer onto its icon and clicking the left mouse button.

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

  1. Open Dr. Scheme in addition to your Internet browser containing this lab.
    1. Consider the following Scheme definition:

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

      Select this material 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.

      In preparation for later in this lab, save the resulting Definitions' Window in Dr. Scheme as the file my-test-file.

    2. Move the mouse to the definitions window of Dr. Scheme, and click the middle mouse button to paste the definition into Dr. Scheme.

    3. Can you paste this definition into Dr.Scheme's Interactions window and into a terminal window?

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 previous CSC 153 labs) 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.

Autocompletion

  1. Try using "autocompletion" with the file my-test-file by typing"cat ~/my-t" followed by a TAB key. You should find that when you press TAB the system completes the (unique) file name for you. Note that on the Campus Linux Network, when you type the tilde character "~" in the terminal window, the character will LOOK like a hyphen "-" instead. Even so, the system will interpret it as a tilde. (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 Dr. Scheme or another editor.

    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.
    Hit TAB a second time to see how the system responds.

    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. For example, try typing his followed by TAB.
    What is the fewest characters you can type to produce the history command?
    What other commands begin with the letter h?
    Are there any commands that begin with her?

Background Processes

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

    First, type

       acroread ~walker/c/examples/examples.pdf
    

    or use autocompletion to reduce your typing! This will open acrobat 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.

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

       acroread ~walker/c/examples/examples.pdf &
    

    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.

Closing the Terminal Window

  1. Close your terminal window with the keyboard shortcut ctrl-d.

Directory and File Commands

The Linux Directory/File Hierarchy

In this section, we will explore part of the Linux file hierarchy.

Pathnames

  1. In a terminal window, type pwd (print working directory) to determine the absolute path name of the current directory.

  2. Type ls . to get a listing of the current directory, and ls .. to get a listing of all files in the parent directory. Note that your current directory should be visible as one item within its parent directory.

  3. The tilde character used alone specifies your home directory, so ls ~ will give a listing of your home directory. When the tilde appears before a name, the combination denotes the home directory before the home directory corresponding to the name. Thus, ls ~walker lists the home directory for user walker.

  4. The top of the Linux file hierarchy is designated by a slash (/) and is called root. Use the command ls / to obtain a listing of all files and directories within the root directory. How many are there?

    In reviewing the files within the root directory, look at the following specific directories:

    root directory hierarchy
  5. Use the commands which and whereis to locate where the acroread program is located:

       which acroread
       whereis acroread
    

File Utilities

Consult the following commands in completing the following steps.

Utility Description
ls "list" files and directories
pwd "print working directory"
cd "change (your working) directory"
mkdir "make directory"
rmdir "remove directory"
cp "copy" a file or directory
mv "move" a file or directory (i.e., rename it)
rm "remove" a file (i.e., delete it)
  1. Within your home directory, create a new directory csc153. Then move to this csc153 directory and create subdirectories scheme, c, labs, and sup-prob.

    Move any CSC 153 files from your home directory to the relevant subdirectory of csc153. As the semester progresses, this organization will help you keep your various files separate.

  2. Move to the labs subdirectory within your csc153 directory. The move to your sup-prob directory with the command

       
      pushd ../sup-prob
    

    Check that the command popd takes you back to the sup-prob subdirectory.

    Write a few sentences that explain the difference between the following commands:

       
      pushd ../scheme
      cd ../scheme
    

Displaying Text Files

  1. Try the following commands that display all or part of this laboratory exercise:

       cat /home/walker/public_html/courses/153.sp09/labs/lab-linux-basics.shtml
       more /home/walker/public_html/courses/153.sp09/labs/lab-linux-basics.shtml
       less /home/walker/public_html/courses/153.sp09/labs/lab-linux-basics.shtml
       head /home/walker/public_html/courses/153.sp09/labs/lab-linux-basics.shtml
       head -n 20 /home/walker/public_html/courses/153.sp09/labs/lab-linux-basics.shtml
       tail /home/walker/public_html/courses/153.sp09/labs/lab-linux-basics.shtml
       tail -n 20 /home/walker/public_html/courses/153.sp09/labs/lab-linux-basics.shtml
    

    (Note that this is a wonderful time to use the arrow keys to edit previous commands rather than to retype the full lines each time.) For the less command, try the arrow keys to move up and down in the file.

The Manual

  1. Use the man to consult the online Linux manual:

    1. What do the following command options mean?

        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 identifyIt the parameters, return type, and "include files" needed for a set of functions related to 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".

Printing

In the interests of saving paper, this lab does not ask you to practice printing files with the lpr and a2ps commands. However, you should review the following table for future reference.

Utility Description
a2ps file
a2ps -Pescher file
a2ps --sides=duplex file
prints file to default printer (handles many standard file formats)
same, for printer named escher
same, but double-sided
lpq
lpq -Pescher
displays jobs in print queue on default printer
same, for printer named escher
lprm 585
lprm -Pescher 585
cancels (removes) print job number 585 from default printer queue
same, for printer named escher

Directory and File Permissions

The following steps ask you to review the permissions for your account directories and adjust them for this course.

  1. Move to your home directory and obtain a "long" listing of the files present using the commands:

       cd
       ls -l
    

    (The cd command without parameters takes you to your home directory.)

    Interpret the meaning of each part of the directory and file listings.

  2. Just for fun, type the whoami command as a type of reassurance that you still are still functioning logically — even with the length of this lab!

Setting Permissions

For CSC 153, it seems likely that you will want others in the class to be able to read your labs, since you are collaborating with others on that material. However, you do not want others to be able to read your supplemental problems. These steps set up this framework.

  1. Move to your home directory. Then allow others to read (but not change) your login directory with the command:

       chmod 755 .
    

    Next allow others to read (but not change) files in your labs directory:

       chmod 755 labs
    

    Now use the ls -l -a command to check that others can read your home directory and the labs subdirectory, but no other directories.

    Team up with another class member to check which directories of theirs you can read.

  2. Now suppose you set your home directory with the command

       chmod 711 ~
    
    1. Can others obtain a listing of your home directory?
    2. Can others obtain a listing of your labs subdirectory?

Setting Terminal Defaults

  1. Review the material present in the .bashrc file in your home directory. Be sure you can explain the purpose of the umask and alias commands.

This document is available on the World Wide Web as

http://www.cs.grinnell.edu/~walker/courses/153.sp09/labs/lab-linux-basics.shtml

created 31 March 2008
last revised 7 April 2008
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu.