DistributedNetworks DistributedNetworks

Shell Components   «Prev 

Regular Expression - Exercise

Practice using regular expressions

Objective:Use regular expressions with common UNIX commands.

Exercise scoring

This exercise is worth 2 points.


You need access to a UNIX command line for this exercise, either through the DistributedNetworks account that you establishedcin Module 1, or though another UNIX system.
The -d option is used with the ls command in this exercise to list the names of directories without listing their contents.


  1. cLog in to your UNIX Lab account (or other UNIX account that you have access to).
  2. Change to the /etc directory (or another directory containing many files on your system).
  3. Use the ls command to list all files in the directory that begin with the letter “s”.
  4. ls -d s*
  5. Use the ls command to list all files in the directory that have “e” as the second letter.
  6. ls -d .e*
  7. Use the ls command to list all files that begin with any one of the letters “h” “i” or “l”.
  8. ls -d [hil]*
  9. Use the grep command with the -c option to count the number of lines in the /etc/termcap file that begin with an “N” (you can leave out the -c option if you want to view the actual lines from the file).
  10. grep -c “^N” termcap

Using quotation marks around a regular expression prevents the shell from interpreting any special characters (like ^) before passing the expression to the grep command. Quotation marks are a good idea if a regular expression is complex or does not appear to be working properly.
  1. Experiment with other regular expressions using the ls and grep commands if you wish. Log out of your UNIX Lab account when you have finished.