4.3. Using [ . . . ] and [ ! . . . ]

Use [...] to match any characters inside the brackets. This method also supports ranges by separating the range with a dash. To list all filenames that start with an ‘i’ or ‘o’:

						$ ls [io]* 
inetd.conf initrunlvl 
inputrc    issue 
info-dir   inittab 
ioctl.save issue.net
					

To match filenames that begin with ‘log.’, followed by a single number, followed by any other character. The [0–9] means any single number. The asterisk takes care of multiple numbers:

						$ ls log.[0-9]* 
log.0323  log.0324 
log.0325  log.0326
					

To list all filenames that have the same criteria as above but with no numbers, use the negation symbol (!) inside the bracket. [!0–9]* means it must not start with a digit.

						$ ls log.[!0-9]* 
log.sybase
					

To match ...

Get Linux and Unix Shell Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.