DirectNET

Data Center Management Solutions including UPS Systems, Data Center Cooling, KVM over IP & IP Power Strips, Server Racks and Server Rack accessories; KVM Switches and KVM Extenders; Rackmount Monitors and Rackmount Keyboards.


NAVIGATION
Home
Store
INSIDE MAC
Television Shows
Broadcast Shows
Daily News Shows
Special Shows
EVENTS
DAILY TIPS
Design
Mac OS X
Mac OS X UNIX
COMMUNITY
Surveys
NEWS
Current
Press
Archive
FEATURES
Editorial
Dr. Mac
Reviews
Reader Reports
RESOURCES
FAQ
Documentation
Learning Center
MAN pages
Glossary
Tutorials
Tips
Links

OUR PARTNERS

OSXFAQ Mac OS X UNIX Tip-of-the-Day  back to index

Spotlight - Boolean Expressions

By Adrian Mayo - Editor - OSXFAQ

Yesterday, we saw how to use mdfind to find files that contain particular text, or have particular metadata. Today we'll look at how we can be more exacting about what words a file should, or should not, contain.

To illustrate, we have three simple text files:

$ cat file1.txt
Kiss me
$ cat file2.txt
Kate
$ cat file3.txt
Kiss me, kate

Let's look for files that contain the word "kiss":

$ mdfind -onlyin ~/splotlight-test "kiss"
/Users/saruman/splotlight-test/file1.txt
/Users/saruman/splotlight-test/file3.txt

Note use of the option -onlyin to limit searches to files rooted in a particular directory hierarchy. You must specify the full path name of the directory.

And files that contain the word "kate":

$ mdfind -onlyin ~/splotlight-test "kate"
/Users/saruman/splotlight-test/file2.txt
/Users/saruman/splotlight-test/file3.txt

If we specify both terms, Spotlight will return those files that contain ALL the terms - in this example "kiss" AND "kate".

$ mdfind -onlyin ~/splotlight-test "kiss kate"
/Users/saruman/splotlight-test/file3.txt

To test for "kiss" OR "kate", we separate the terms with '|'.

$ mdfind -onlyin ~/splotlight-test "kiss|kate"
/Users/saruman/splotlight-test/file1.txt
/Users/saruman/splotlight-test/file2.txt
/Users/saruman/splotlight-test/file3.txt

There must be no white space around the '|' symbol.

What about not kissing kate?

$ mdfind -onlyin ~/splotlight-test "kiss(-kate)"
/Users/saruman/splotlight-test/file1.txt

Again, make sure there is no white space between the terms.

And finally, "kiss" or "kate", but not "me".

$ mdfind -onlyin ~/splotlight-test "kiss|kate(-me)"
/Users/saruman/splotlight-test/file2.txt

Tomorrow, we'll combine mdfind with regular Unix commands such as find, grep, and xargs.


Visit the Site of the Book of the Unix Tips:


Discuss this trick in the OSXFAQ Learning Center forum

E-mail your comments or suggestions to webmaster@osxfaq.com


Copyright © 2000-2010 Inside Mac Media, Inc. All rights reserved.
Apple assumes no responsibility with regard to the selection, performance, or use of the products or services. All understandings, agreements, or warranties, if any, take place directly between the vendors and prospective users.
Apple, the Apple logo, Mac, PowerMac G4, PowerMac G5, Xserve, Xserve RAID, PowerBook, iBook, Airport, AirPort Extreme, iMac, eMac, iLife, iMovie, iCal, iPhoto, iTunes, QuickTime, FireWire, iPod, iSight, AppleWorks, Macintosh, Jaguar, Panther, Mac OS, Mac OS X and Mac OS X Server are trademarks of Apple Computer, Inc.