Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Table of Contents


Pathname globbing

Since another goal of Unix is to type as little as possible, there are several metacharacters that serve as wildcards to represent sets of characters when typing file names. Using these metacharacters is called globbing (don't ask me why (smile)) and the pattern is called a glob.

  • asterisk ( * ) is the most common filename wildcard. It matches any length of any characters.
  • brackets ( [ ] ) match any character between the brackets.
    • and you can use a hyphen ( - ) to specify a range of characters (e.g. [A-G])
  • braces ( {  } ) enclose a list of comma-separated strings to match (e.g. {dog,pony})

And wildcards can be combined. Some examples:

Code Block
languagebash
ls *.txt        # lists all files with names ending in ".txt"
ls [a-z]*.txt   # does the same but only lists files starting with a lowercase letter
ls [ABhi]*      # lists all filenames whose 1st letter is A, B, h, or i
ls *.{txt,tsv}  # lists filenames ending in either .txt or .tsv

Exercise 2-6

Design a wildcard that will match the files haiku.txt and mobydick.txt but not jabberwocky.txt.

Expand
titleAnswer...

There are always multiple ways of doing things in Unix. Here are two possible answers:

Code Block
languagebash
ls [hm]*.txt
ls {haiku,jabberwocky}.txt 


x