CSC201: Memory Management, Data Representation, and Formal Methods
Grinnell College


Laboratory: Shell Commands

Summary: In this laboratory you will become more familiar with the bash shell and some of the operations it supports.

Prerequisites: Familiarity with the MathLAN network

Contents:

Exercises

Exercise 0: Preparation

Open a terminal window.

The aspect of the Linux operating system that you interact with when you type commands into a terminal window is called the shell. In the MathLAN, we use a particular shell (among several that are supported by most versions of Unix) called the bash shell.

Exercise 1: Displaying Text Files

It is often convenient to look at the contents of a text file without having to open it in an editor. The following commands allow you to do so.

cat - concatenate and display text file(s)
head - display first few lines
tail - display last few lines
more - display one page at a time
less - new and improved more

a. Try the command "cat welcome.c". You should have another short c program, perhaps called sqrt.c. Try "cat welcome.c sqrt.c".

b. Try the other commands listed above on a longer text file, perhaps a java program from a previous course.

For more, try pressing spacebase, enter, and 'q'.

For less, try pressing the up/down arrows and PageUp/PageDown as well.

Exercise 2: More Fun with Text Files

wc - count the lines, words, and characters in a text file
diff - display the differences between two text files
grep - search for a target string in a text file
sort - sort lines in a text file (original is unchanged)
uniq - remove duplicate lines (original is unchanged)

a. Sometimes it is nice to know how many lines of code we have written. Try "wc welcome.c".

b. More importantly, we may need to know what has changed between one version of a program and another.

Since you have been programming in emacs, you probably have some files whose names end in ".c~". (When you save a file in emacs, the previous version is retained in another file with "~" appended to its filename. If you don't have such a file in your directory, create one by editing welcome.c and saving your changes.)

Now list the differences between the two versions with a command like "diff welcome.c welcome.c~".

c. It can be extremely handy to find all the instances of a given variable name (or other text string) in a text file.

Try "grep include welcome.c".
Try "grep include *.c".
Try "grep in *.c".

Exercise 3: Other Fun Commands

Give these a try. What do they do?

  whoami
  which emacs
  history
  clear

Exercise 4: The Manual

Unix includes an on-line help system, called the manual. There is a "man page" describing each Unix command and C library function. For example, try the following. (You may notice that the man pages often have a certain geeky sense of humor about them.)

   man less
   man sqrt

Don't worry if you don't understand everything you see in a man page. They are often both long and cryptic, but you can learn a lot from them without having to understand everything!

One very handy use for the man pages is looking up which header file you need to #include in order to use a function in the C library. Now suppose you wanted to look up the square root function, but you didn't remember its name. To print a list of man pages for which the word "square" is included in the command name or description, use this:

   man -k square

Exercise 5: I/O Re-direction

a. You can direct the output from a command, or other program, to be written to a file. For example, try these:

   ls > dirlist.txt
   ./a.out > output.txt

They should create new files in your directory, which you can view with the commands you used above.

Now try the following. What is the difference between what the two symbols > and >> do? (You may want to try both with the same output file a few times to be sure.)

   ls >> output.txt

b. You can also direct a command, or program, to receive its input from a file with the < symbol. We will explore this further once we start writing programs that expect to receive input.

c. You can use a pipe (i.e., the vertical bar symbol toward the upper right of the keyboard) to direct the output of one command to the input of another. Here are some examples. (Predict what they will do, and then give them a try. Did they do what you expected?)

  history | tail
  ls -l *.c | wc

Exercise 6: Making a "Tarball"

The Unix shell command tar allows you to "bundle" several files into a single file. The resulting file typically has the extension .tar", and it is called a "tar file" or sometimes a "tarball." Tar files are a very convenient way to transmit a group of files (as a single file) via email, ftp, etc.

a. Try the command "tar --help | less". Some shell programs also provide help in this form, and I particularly like the examples provided at the top of the tar help text.

b. Make a tar file, called lab1.tar that contains at least two of your source code files from the previous lab. (You will find the first example in the tar help text useful for this.)

Use the command "ls -ltr" to confirm that a tar file was created.

c. Create a new directory (with the command mkdir), called something like tartest. Move (mv) or copy (cp) your tar file into that directory, and then "untar" (i.e., extract the files from) the tar file. The third example in the tar help text will be useful here.

Confirm that your source code files were extracted into your tartest directory.

d. You may have noticed that tar has much the same functionality as zip, which is commonly used on Microsoft systems. In fact, Unix systems can also zip and unzip, which is convenient for sharing files with Microsoft users.

I will probably ask you to create a tar file (or zip file) when you submit your homeworks. That will be more convenient for both of us than having you submit multiple individual files.

Exercise 7: Navigating the File System

You may already be familiar with the following file management commands. If not, now would be a good time to look them up and start using them!

  ls
  cd
  pwd
  cp
  mv 
  mkdir
  rmdir
  rm 
  chmod

I find the following variants handy.

  ls -l
  ls -ltr
  cp -p

I also prefer moving files to a directory named ~/trash, rather than deleting them outright. (It is far too easy to delete all your hard work in one simple rm command. Unfortunately, Unix will do what you tell it to.)

I also like to create separate directories for files associated with separate courses. It's a nice way to stay organized!


Written by Marge M. Coahran, January 2007.