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 - Piping its Output

By Adrian Mayo - Editor - OSXFAQ

Just like the regular Unix find, the list of files returned by mdfind can be piped to other Unix commands for further processing.

The next command will filter the results from find leaving only files that have the extension ".pdf".

$ mdfind "kiss kate" | grep -i "\.PDF$"

We escaped the dot because it has a special meaning to regular expression, and used the dollar symbol to anchor matching text to the end of the term (i.e. matching filenames must end in .pdf, not just have .pdf in the middle).

To open all the files in Preview type:

$ mdfind -onlyin /Users/shared/books "kiss kate" | xargs open

To make the search term more selective, use grep and regular expressions. Here's a simple example:

$ cat file1.txt
Kiss me $ cat file3.txt
Kiss me, kate $ mdfind -onlyin /Users/saruman/splotlight-test "kiss me,"
/Users/saruman/splotlight-test/file1.txt
/Users/saruman/splotlight-test/file3.txt

The comma is ignored by spotlight. Let's use grep:

$ mdfind -onlyin /Users/saruman/splotlight-test "kiss me," | xargs grep -i "Kiss me,"
/Users/saruman/splotlight-test/file3.txt:Kiss me, kate
grep: /Users/saruman/splotlight-test/file: No such file or directory
grep: 4.txt: No such file or directory
$

(Tip: Use grep to make the search term case sensitive.)

The command choked on the filename "file 4.txt" because it contains a space. Specify option -0 (zero) to both mdfind and xargs to fix this.

$ mdfind -0 -onlyin /Users/saruman/splotlight-test "kiss me," | xargs -0 grep -i "Kiss me,"
/Users/saruman/splotlight-test/file3.txt:Kiss me, kate
/Users/saruman/splotlight-test/file 4.txt:Kiss me, kate
$

This is the same trick as used with "find | xargs" as is described here.


Visit the Site of the Book of the Unix Tips:
Project 23 discusses grep. Find and xargs are explored in depth in Projects 15, 17, and 18. Projects 77 and 78 teach regular expressions.


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.