Wildcards and What They Mean


Imagine that you have the following files12 pictures and a text filein a directory on your computer:

libby1.jpg libby2.jpg libby3.jpg libby4.jpg libby5.jpg libby6.jpg libby7.jpg libby8.jpg libby9.jpg libby10.jpg libby11.jpg libby12.jpg libby1.txt 


You want to delete these files using the rm command (covered in Chapter 2) on your command line. Removing them one at a time would be tedious and kind of silly. After all, one of the reasons to use computers is to automate and simplify boring tasks. This is a job for wildcards, which allow you to specify more than one file at a time by matching characters.

There are three wildcards: * (asterisk), ? (question mark), and [ ] (square brackets). Let's take each one in order.

The * matches any character zero or more times. Table 1.2 has some uses of the * and what they would match.

Table 1.2. The Wildcard * and What It Matches

Command

Matches

rm libby1*.jpg

libby10.jpg through libby12.jpg, as well as libby1.txt

rm libby*.jpg

libby1.jpg through libby12.jpg, but not libby1.txt

rm *txt

libby1.txt, but not libby1.jpg through libby12.jpg

rm libby*

libby1.jpg tHRough libby12.jpg, and libby1.txt

rm *

All files in the directory


The ? matches a single character. Table 1.3 has some uses of the ? and what they would match.

Table 1.3. The Wildcard ? and What It Matches

Command

Matches

rm libby1?.jpg

libby10.jpg through libby12.jpg, but not libby1.txt

rm libby?.jpg

libby1.jpg through libby9.jpg, but not libby10.jpg

rm libby?.*

libby1.jpg though libby9.jpg, as well as libby1.txt


The [ ] match either a set of single characters ([12], for instance) or a range of characters separated by a hyphen (such as [1-3]). Table 1.4 has some uses of the [ ] and what they would match.

Table 1.4. The Wildcard [] and What It Matches

Command

Matches

rm libby1[12].jpg

libby11.jpg and libby12.jpg, but not libby10.jpg

rm libby1[0-2].jpg

libby10.jpg through libby12.jpg, but not libby1.jpg

rm libby[6-8].jpg

libby6.jpg through libby8.jpg, but nothing else


You'll be using wildcards all through this book, so it's good to introduce them now. They make dealing with files on the command line that much easier, and you're going to find them extremely helpful.



Linux Phrasebook
Linux Phrasebook
ISBN: 0672328380
EAN: 2147483647
Year: 2007
Pages: 288

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net