Using Wildcards


You might think of wildcards as being placeholders for omitted letters or numbers. For example, if you're looking for a file but aren't sure whether you named it kidnews or kidupdate, you can include a wildcard to stand for the part you're uncertain of. That is, you could list the files of a directory with ls kid* (Code Listing 1.7), which would list all files starting with the characters "kid". In the resulting list, you'd find a file named kid if there were one, as well as files that begin with kid but have varying endings, such as kidnews (aha, the lost file!), kiddo, or kidneypie.

Code Listing 1.7. You use wildcards (? or *) to act as placeholders for missing characters.

[jdoe@frazz Project]$ ls keep keeper.jpg keptkidder.txt  kiddo kidnews kidneypie  kids kidupdate [jdoe@frazz Project]$ ls ki kidder.txt kiddo kidnews kidneypie  kids kidupdate [jdoe@frazz Project]$ ls kid* kidder.txt kiddo kidnews kidneypie  kids kidupdate [jdoe@frazz Project]$ ls k??? keep keptkids [jdoe@frazz Project]$ ls *date kidupdate [jdoe@frazz Project]$ ls *up* kidupdate [jdoe@frazz Project]$ ls k?d* kidder.txt kiddo kidnews kidneypie  kids kidupdate [jdoe@frazz Project]$ 

You can use wildcards for just about any purpose in Unix, although listing files and directories will likely be the most common use. Just follow these guidelines:

  • You use ? as a placeholder for one character or number.

  • You use * as a placeholder for zero or more characters or numbers. Zero characters, in case you're curious, specifies that the search results include all variants of kid, including the word itself with no suffix.

  • You can include a wildcard at any place in a name: at the beginning (*kid), some where in the middle (k*d), at the end (ki*), or even in multiple places (*kid*).




Unix(c) Visual Quickstart Guide
UNIX, Third Edition
ISBN: 0321442458
EAN: 2147483647
Year: 2006
Pages: 251

Similar book on Amazon

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