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

Linux and C


This lab continues our exploration of Linux, including pipes, filters, additional Linux utilities, wildcards, quoting, and command substitution.


Before starting work in the lab, be sure you have read the following materials:

Pipes and Filters

  1. Explore the use of the ls command to obtain a long directory listing.

    1. Type ls -l in a terminal window. Describe what information is displayed and how the data are organized.

    2. In a terminal window, type man sort to obtain information about the sort command. Based on this information, determine what the command sort -k 6 does.

    3. Type the command ls -l | sort -k 6 in a terminal window. Again, describe what information is displayed and how the data are organized.

    4. Type the command ls -l | sort -k 6 | more in a terminal window. (You can scroll through the data page at a time by hitting the space bar.) Explain the role of the last pipe | more in this line.

Additional Linux Utilities

The following table is repeated from the reading for today's lab. All of these filters can accept their input from regular files, stdin, or a pipe. None of them modify their original input; rather, they generate new output that reflects a modified version of the input.

Utility Description Example usage
wc "word count" - counts characters, words, and lines in input wc -l ~/.bashrc
sort sorts lines in input sort -k2 ~coahranm/share/csc201/sciencefac.txt
uniq "unique" - removes (or reports) duplicate lines in input uniq ~coahranm/share/csc201/duplicates.txt
grep searches for a target string in input grep li ~coahranm/share/csc201/duplicates.txt
cut removes parts of lines from input cut -d' ' -f2 ~coahranm/share/csc201/sciencefac.txt
diff reports the differences between two files diff ~walker/public_html/courses/153.sp09/labs/lab-linux-c.shtml ~walker/public_html/courses/153.sp09/labs/lab-linux-c.shtml~
  1. Use the filters given above (and other utilities if needed) to perform the following tasks. Note that for some of the tasks you may need to combine filters with pipes.

    1. Count the lines of source code in a program you wrote earlier this semester for this course.

    2. Determine the number of user accounts on the MathLAN. Recall that each account has a directory in /home.

    3. Print a list (in the terminal window, not on a printer) of 2007-2008 faculty in Grinnell College's Science Division, sorted by last name. The file ~coahranm/share/csc201/sciencefac.txt contains the data you need.

    4. Print a list of 2007-2008 faculty in the Biology Department. Your list should not include faculty in any other department.

    5. Consider one of the programs you wrote earlier this semester. Take a quick look at it, using less to remind yourself of a variable name that is used in several places in the file. Now use grep to print a listing of the lines that include that variable. Get grep to print the line number (in the source file) for each line of output as well.

      Note that it can be very useful to use grep in this way when you return to a project after taking a long break from it. For example, you might want to find every instance of a given class -- in any source file in the project -- as part of re-acquainting yourself with your code.

    6. Print a list of all 2007-2008 Grinnell faculty named David.

      Hint: To do this, it would be helpful to create a single list that combines all the entries in the three faculty lists I have provided. But instead of generating a separate combined file, you can do this on the fly using cat as shown below. This is where cat gets its name -- from its ability to concatenate multiple files.

      cat  ~coahranm/share/csc201/socialfac.txt ~coahranm/share/csc201/humanfac.txt  ~coahranm/share/csc201/sciencefac.txt
    7. Print a unique list of departments in the Humanities Division.

    8. The names provided in ~coahranm/share/csc201/ give the 2007-2008 faculty for the various departments, and the Chair of each department is denoted by an asterisk. Use grep to output a list of Department Chairs in one (or all) of the divisions. It might also be nice to sort your list by last name.

A Few More Useful Linux Capabilities

  1. Ms. Coahran has the following file in her share directory:

       ~coahranm/share/csc201/goofy file name
    1. What happens if you try to list its contents using the following command? Give it a try to be sure.

         cat ~coahranm/share/csc201/goofy file name
    2. Modify the above command in at least two ways to obtain the desired listing.

Command Substitution

  1. Try command substitution with backquotes, by looking up any of the following commands you are not familiar with already.

      echo There are `ls | wc -l` files in my current working directory.
      echo Today is `date +%A`. It is now `date +%r`.

This document is available on the World Wide Web as

created January 2007 by Marge Coahran
revised January 2008 by Marge Coahran
revised 10 April 2008 by Henry M. Walker
revised 3 April 2009 by Henry M. Walker
reorganized 2 April 2010 by Henry M. Walker
last revised 3 April 2010 by Henry M. Walker
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at