CSC 161 Grinnell College Spring, 2012
 
Imperative Problem Solving and Data Structures
 
 

Basic Linux Commands and Capabilities — 2

Abstract

This laboratory exercise continues the previous lab to review basic commands and capabilities that match likely needs of beginning CSC 161 students. In the subsequent lab, you will learn a few more basic commands and also prepare for compiling programs for use with the robots.

Introduction

This lab continues work with the following basic capabilities and commands.

Topic Category Subtopics Linux Commands
Directories paths ., .., ~, /, pwd
pathnames absolute, relative, pwd, cd, ls, which, whereis
search paths search path variable $PATH
setting terminal defaults .bashrc
Myro Setup Myro .bash Setup
C_INCLUDE_PATH
LIBRARY_PATH
LD_LIBRARY_PATH
export
Files file utilities mkdir, rmdir, rm, cp, mv, more, head, tail, pushd, popd
Printing printing lpr, a2ps, lpq, lprm

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

The Terminal Window and the Manual

As with the previous lab, most work for this lab involves experimentation with a terminal window.

Directory Commands

The Linux Directory

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

Pathnames

  1. Current Directory
    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.

  2. Home Directory

    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.

  3. Linux File Hierarchy

    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:

    1. /bin: These are the executable programs that comprise the GNU/Linux utilities. For example, there is an executable file here named ls that is run when you issue the command ls.

    2. /home: You won't be surprised to hear that user accounts are stored in this directory.

    3. /lib: This directory is the home of many libraries that can be used by programmers. For example, you should be able to find a file named libc-2.3.6.so here, that contains the "standard c library functions" we will use later in the course.

    4. /usr: The name of this directory is pronounced "user", and it generally contains application programs, libraries, and other files that are not part of the GNU/Linux system (i.e., optional resources intended for and requested by users). For example, the acrobat reader is located here under /usr/bin/evince.

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

       which evince
       whereis evince
    

    What is the difference between the two commands?

Search Paths

The system sets up many elements for you when you log in, and additional set up may occur whenever you open a window. For example, the operating system processes a file called .bashrc in your home directory each time it opens a terminal window.

One of these elements is an environmental variable $PATH that indicates where the operating system should look for various commands.

  1. Within a terminal window, type the command

       echo $PATH
    

    Examine the output of this command to determine where the operating system looks for various commands and in what order. Within this string, directories are separated by colons (:).

    1. Write down the listing of directories that are searched when you enter a command into a Linux window. For example, what directory is searched first?
    2. Consider the echo command. Use the ls command to determine if an echo program is located in the first directory on the path. If not, look at the second directory, etc., until you find where ls is located.
    3. Examine the search path printed to determine if the current directory (represented as .) is listed. If the current directory is NOT in your search path, it will be helpful later in the course if we add . to your search path.

      To add . to your search path, open your .bashrc file in any editor and add the following lines:
      
      # Debian Linux does not appear to include the current directory in the
      # search path, so we'll expand the search path:
      
      PATH=$PATH:.
      

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

Myro Setup

Setting Up Your .bashrc File

Although the following instructions may seem confusing to you right now, it will make more sense as the semester progresses. These next steps are necessary for you to be able to program with the Scribbler 2 throughout the course.

  1. Add the following lines to the bottom of your .bashrc file:

    # set environmental variables for Myro C and Myro C++
    
    ### MYRO LIBRARIES ###
    
    # determine if machine is 32-bit or 64-bit
    
    # 64-bit machines in Science 3815
       science3815a='blanch|book|boole|chapanis|even|fasenmyer|flowers'
       science3815b='forsythe|hollerith|landin|mauchly|motwani|noyce'
       science3815c='stockmeyer|strachey|wang|wheeler|wijngaarden|wilkins'
       science3815=$science3815a'|'$science3815b'|'$science3815c
    
    # 64-bit machines in Science 3819
       science3819a='bellman|bollee|burroughs|frege|goedel|harary|householder'
       science3819b='kleene|peano|ritchie|mccarthy|rasiowa|rosser|tarski'
       science3819c='taussky|turing|wilkinson|zermelo'
       science3819=$science3819a'|'$science3819b'|'$science3819c
    
    # 64-bit machines in Science 3828
       science3828='cray|petri'
    
    # 64-bit machines in Science 3830
       science3830='mathews|pnueli'
    
       list64=$science3815'|'$science3819'|'$science3828'|'$science3830
    
    if [[ `hostname` =~ $list64 ]]; then
    
    # use 64-bit environment for MyroC
    
       ## Myro C -- When using libMyroC.so ##
       # include the location of the MyroC header #
       C_INCLUDE_PATH="$C_INCLUDE_PATH:/home/walker/Myro64/include/MyroC"
    
       # include the location of the MyroC shared library object file #
       LIBRARY_PATH="$LIBRARY_PATH:/home/walker/Myro64/lib"
    
       # make the libraries know to the execution environment #
       LD_LIBRARY_PATH="$LS_LIBRARY_PATH:/home/walker/Myro64/lib"
    
    else
    #  use 32-bit environment for MyroC
    
       ## Myro C -- When using libMyroC.so ##
       # include the location of the MyroC header #
       C_INCLUDE_PATH="$C_INCLUDE_PATH:/home/walker/Myro/include/MyroC"
    
       # include the location of the MyroC shared library object file #
       LIBRARY_PATH="$LIBRARY_PATH:/home/walker/Myro/lib"
    
       # make the libraries know to the execution environment #
       LD_LIBRARY_PATH="$LS_LIBRARY_PATH:/home/walker/Myro/lib"
    
    fi
    export C_INCLUDE_PATH LIBRARY_PATH LD_LIBRARY_PATH
    

The basic idea of this insertion is to define three environmental variables which will allow you to compile cleanly with the robots. With this insertion, when we start using the robots, you'll be ready to compile and run your programs!

An aside, in case you care

The complexity of this inserted material arises because MathLan has two basic types of computers. The computers in Science 3815, 3819, and a few other locations use chips and circuits that store 64 binary digits (more about this later in the course). For technical reasons, these machines utilize 64-bit instructions behind the scenes. All other computers in MathLAn utilize 32-bit instructions.

To run programs with the robots, various commands must use the correct 32-bit or 64-bit details. The above instructions check which workstation you are working at, and set up needed variables appropriately.

If you want to use these definitions as soon as you change your .bashrc file, you will need to restart your Bash shell (terminal) so that all the updates are applied. Alternatively, you can use the command: $ source .bashrc in the terminal window. (In any case, in the future, whenever you open a new window, these definitions will be ready for you.)

Now we'll explore statements you just pasted into your .bashrc file in more detail.

C_INCLUDE_PATH

This tells the compiler where to look for the MyroC header files (which is a big list of robot functions).

LIBRARY_PATH

This tells the compiler where to look for the MyroC libraries (which tells the computer how to do the robot functions listed in the MyroC header files).

LD_LIBRARY_PATH

This tells the operating system where to look for the libraries when it's running your programs.

export

This statement exports all of your new environment variables into any future instances of your Bash shell.

By setting these environmental variables here, the computer will know where to find these packages when you work with the Scribbler 2 robots.

File Commands

File Utilities

Consult the following commands in completing the next steps of this lab:

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)

Navigating Directories

  1. Within your home directory, create a new directory csc161. Then move to this csc161 directory and create subdirectories scheme, c, labs, and sup-prob.

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

    2. Move to the labs subdirectory within your csc161 directory. Then move to your sup-prob directory with the command:

         pushd ../sup-prob
      
    3. Check that the command popd takes you back to the labs subdirectory you were in before your change to sup-prob.

    4. 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/161.sp09/labs/lab-linux-basics-2.shtml
       more /home/walker/public_html/courses/161.sp09/labs/lab-linux-basics-2.shtml
       less /home/walker/public_html/courses/161.sp09/labs/lab-linux-basics-2.shtml
       head /home/walker/public_html/courses/161.sp09/labs/lab-linux-basics-2.shtml
       head -n 20 /home/walker/public_html/courses/161.sp09/labs/lab-linux-basics-2.shtml
       tail /home/walker/public_html/courses/161.sp09/labs/lab-linux-basics-2.shtml
       tail -n 20 /home/walker/public_html/courses/161.sp09/labs/lab-linux-basics-2.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. You can also use the tab autocomplete utility.)

    For the less command, try the arrow keys to move up and down in the file.

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 --sides=duplex file
prints file to default printer (handles many standard file formats)
same, but double-sided
lpr
send file to the default printer
lpq
displays jobs in print queue on default printer
lprm 585
cancels (removes) print job number 585 from default printer queue

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.