CIS241

System-Level Programming and Utilities

grep and wildcards

Erik Fredericks, frederer@gvsu.edu
Fall 2025

Based on material provided by Erin Carrier, Austin Ferguson, and Katherine Bowers

top-corner (gurps)

CIS241 | Fredericks | F25 | 9-grep

grep - pattern-matching search/filter

Usage: grep string filename

Common options:

  • -w: Search only whole words
    • Removes partial matches, -w abc does not match abcdef
  • -r: recursive (search files in directory)
  • -i: ignore case
  • -n: print line number

More with regex (later)

CIS241 | Fredericks | F25 | 9-grep

Wildcards

Probably have talked about these already, but formally...

Pattern matching for filenames!

  • * - matches any number of characters
    • ls *.txt
  • ? - matches any single character
    • ls data_?.csv
      • Matches data_7.csv but not data_17.csv
CIS241 | Fredericks | F25 | 9-grep

Wildcards

  • [] - character class or range

    • [abc], [a-z], [[:upper:]], etc...
      • Note the range is per character
    • [!a] for complement
      • ls [[:upper:]]*
  • This is all called globbing!

    • man 7 glob

https://tldp.org/LDP/abs/html/globbingref.html

CIS241 | Fredericks | F25 | 9-grep

Some practice!

Pop open the manual on glob --> man glob

  • Remember, q gets you out of it

Oh no we have some things to look for!

CIS241 | Fredericks | F25 | 9-grep

Some practice - search for the following phrases

  • Search for: save processing time

    • man glob | grep "save processing time"
  • Search for: all uppercase words

    • man glob | grep "[[:upper:]]"
  • Search for glob()

    • man glob | grep "glob()"
CIS241 | Fredericks | F25 | 9-grep

More practice!

Create a temporary directory

  • mkdir temp
  • cd temp

Create some files in it

for i in {1..200}
do
  touch "file_$i"
done

ls
CIS241 | Fredericks | F25 | 9-grep

More practice!

List all files that start with f:

  • ls f*

List all files that start with file_50:

  • ls file_50*

List all files in the range of 5-99:

  • Oops need brace expansion -- only 1 character handled at a time
  • ls file_{[5-9],[1-9][0-9]}
    • This says, either file 5-9, or file 10-99
CIS241 | Fredericks | F25 | 9-grep

for i in {1..200}; do touch "file_$i" done ls f* ls file_10* ls file_[0-9]

make sure it matches the pattern otherwise nothing will happen! create TEST in files folder ls [[:lower:]] <- why doesn't this work ls [[:lower:]]* ls [[:upper:]]* touch 1_TEST ls *[[:upper:]]*