The next few commands show you how to use shell
metacharacters with the ls command to match filenames. Try the following commands to see if
you get the same responses:
$ ls a*
apple
$ ls g*
grape
grapefruit
$ ls g*t
grapefruit
$ ls *e*
apple grape grapefruit watermelon
$ ls *n*
banana watermelon
The first example matches any file that begins with an a (apple). The next example matches any
files that begin with g (grape, grapefruit). Next, files beginning with g and ending in t are
matched (grapefruit). Next, any file that contains an e in the name is matched (apple, grape,
grapefruit, watermelon). Finally, any file that contains an n is matched (banana, watermelon).
Here are a few examples of pattern matching with the question mark (?):
$ ls ????e
apple grape
$ ls g???e*
grape grapefruit
The first example matches any five-character file that ends in e (apple, grape). The second
matches any file that begins with g and has e as its fifth character (grape, grapefruit).
70
Linux First Steps Part I
Here are a couple of examples using braces to do pattern matching:
$ ls [abw]*
apple banana watermelon
$ ls [agw]*[ne]
apple grape watermelon
In the first example, any file beginning with a, b, or w is matched. In the second, any file that
begins with a, g, or w and also ends with either n or e is matched.
Pages:
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227