![]() |
| |||||||
|
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 openTo make the search term more selective, use grep and regular expressions. Here's a simple example: $ cat file1.txtKiss 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:
Discuss this trick in the OSXFAQ Learning Center forum E-mail your comments or suggestions to webmaster@osxfaq.com
|
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||