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

Basic Linux Commands and Capabilities — 2


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.


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

Topic Category Subtopics Linux Commands
Directories and Files paths ., .., ~, /, pwd
pathnames absolute, relative, pwd, cd, ls, which, whereis
search paths search path variable $PATH
file utilities mkdir, rmdir, rm, cp, mv, more, head, tail, pushd, popd
Help manual man
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

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

Directory and File Commands

The Linux Directory/File Hierarchy

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


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

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

      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

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

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

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


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
displays jobs in print queue on default printer
lprm 585
cancels (removes) print job number 585 from default printer queue

Reminder: Complete Evaluation Form

When you have finished this lab, be sure to fill out its evaluation form in the "Lab Evaluation" section for CSC 161 on Pioneer Web.

This document is available on the World Wide Web as

created 31 March 2008 by Henry M. Walker
last full revision 18 July 2011 by Erik Opavsky and David Cowden
minor editing 24 August 2011 by Henry M. Walker
last revised 30 August 2011 by Henry M. Walker
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at