What To Do When Spotlight Isn’t Enough

by James R. Stoup Feb 20, 2007

Personally, I love Spotlight. I think it is a great addition to the Finder in OS X. However, there may be times when you need a little bit more than what Spotlight can offer you. In those cases I would recommend that you go to File > Find . . . and use this more detailed searching utility.

But James, you say, I need more searching capabilities. I need more complexity. In short, I need more power. Well, in that case, I would once again recommend you go have a look at the Find . . . utility. But if you started to nag me and whine about how you just don’t need more stress in your life then I would introduce you to the command line tool called grep. In fact, it would go something like this:

Me: User, this is grep. Grep, this is user.
You: Hi, nice to meet you.
Grep: Screw you.

And that should tell you all you need to know about this wonderful utility. Grep is a very powerful searching tool; however, it is by no means easy to use. Before we get much farther though, I need to explain what grep does. Grep takes an input and searches inside of files for matches. In this regards it is much like Spotlight, though grep is much more versatile. For example, with Spotlight you can search your system for the phrase “genital herpes” and have a list returned back to you of all the files that contain that phrase. However, with grep you can search for files that contain the phrases “genital herpes” AND “mary” but does NOT contain the phrase “jack.” You can then sort these results by file size, date of creation, date of last modification, etc. Very cool, no?

So, let’s start with a basic tutorial. Go and open the Terminal. You can use Spotlight if you don’t know where it is or you can look in Applications/Utilities for it. Ok, once you have it opened you should see a prompt that looks something like this:

james-r-stoups-imac:~ jrstoup$ 

This should tell you the name of the user, james-r-stoups-imac, and the directory you are currently in, jrstoup. Starting up for the first time should automatically put you in your Home folder. To double check this, type in the command to list all files in the current directory, which is “ls.”

james-r-stoups-imac:~ jrstoup$ ls
Desktop       Documents     Library       Movies       Music       Pictures     Public       Sites
james-r-stoups-imac:~ jrstoup$

And sure enough you should see everything that is in your home directory. Now, we are going to go looking for something using grep, but before we do, we need to know how to use it. Well, grep requires three pieces of information to work. The text to search for, where to look, and what options to use while looking.

grep [phrase to search for] [location to search] [options to use while searching]

Here is an example:

james-r-stoups-imac:~ jrstoup$ grep neil * -r -i -I          
Desktop/book lists.rtf:\cf0 Neil Gaiman
Desktop/book lists.rtf:\f2\b0 \cf0 Neil Gaiman
Desktop/book lists.rtf:\f2\b0 \cf0 Neil Gaiman
Desktop/book lists.rtf:\f2\b0 \cf0 Neil Gaiman
Library/Caches/Firefox/Profiles/85snpsc1.default/Cache/4C123132d01:  a href=”/photos/adventurerneil/”>adventurerneil says:
Music/iTunes/iTunes Music Library.xml:           

Comments

Historians referenced in the episode: Gibbons, Vegetius, Bark, Toynbee, Ward-Perkins, Heather, McNeill, Bury
james-r-stoups-imac:~ jrstoup$

The above command can be read as follows: Look for the string “neil” in all of the files in the current directory (*), then search recursively (-r) through the rest of the system, search upper and lowercase (-i), and ignore binary files (-I). If you want more options for using grep, type “man grep” into the command line and use the up and down arrows to see all of the different flags. Type “q” to get out of the manual pages and back to the terminal.

So, now that you know how to use grep, let’s look at the output. Unless you tell it differently, grep will return the path to the file and each occurrence of your search text. So, how you can read this is that the file “book list.rtf,” which is located on the Desktop (Desktop/book lists.rtf), contains four instances of the word “neil” (in this case it was Neil Gaiman). There was one instance in the Library directory (adventurerneil) and one in iTunes (McNeill).

But what if I wanted to search for files containing “neil,” and then search in those results for files that also had “gaiman” in them? Well, you would use a pipe to redirect the output of one grep into another grep. The pipe symbol is “shift+\.”

gjames-r-stoups-imac:~ jrstoup$ rep neil * -riI | grep gaiman -i
Desktop/book lists.rtf:\cf0 Neil Gaiman
Desktop/book lists.rtf:\f2\b0 \cf0 Neil Gaiman
Desktop/book lists.rtf:\f2\b0 \cf0 Neil Gaiman
Desktop/book lists.rtf:\f2\b0 \cf0 Neil Gaiman
james-r-stoups-imac:~ jrstoup$

Cool, huh?

Now let’s do something a little different. I am going to search for the string “digg,” only this time I’m including the “-l” flag which will print me out a list of files instead of the instances of occurrences.

james-r-stoups-imac:~ jrstoup$ grep digg * -r -i -I -l
Desktop/AM top 20.rtf
Desktop/digg.rtf
Documents/Homeworks Related Items/Homeworks Video Productions/About_Info.htm
Library/Application Support/Adium 2.0/Users/Default/Logs/AIM.stepbasin/applematters/applematters (2007-01-24).html
Library/Application Support/Firefox/Profiles/85snpsc1.default/history.dat
Library/Application Support/Firefox/Profiles/85snpsc1.default/sessionstore.js
Library/Caches/Firefox/Profiles/85snpsc1.default/Cache/50F4E13Ed01
Library/Caches/Firefox/Profiles/85snpsc1.default/Cache/9FCF7D63d01
Library/Caches/Firefox/Profiles/85snpsc1.default/Cache/F1AA1451d01
Library/Caches/Firefox/Profiles/85snpsc1.default/Cache/F5B343DCd01
Library/Caches/Firefox/Profiles/85snpsc1.default/Cache/FFCA3BA0d01
Library/Caches/Firefox/Profiles/85snpsc1.default/Cache/FFCA3BB0d01
Library/Cookies/Cookies.plist
Library/Favorites/Documents/Homeworks Related Items/Homeworks Video Productions/About_Info.htm
Music/iTunes/iTunes Music Library.xml
james-r-stoups-imac:~ jrstoup$

There you go, a quick lesson on grep. An invaluable tool for the command line junkie, yet a curiosity for everyone else. See, I told you to use Find. . .

Comments

  • My major complaint with Spotlight is that it skips invisible files and app/system components which I occasionally need to find to clean up an uninstall or misbehaving application.  It would be nice if Spotlight would allow a simple switch to these advanced features that would only confuse ordinary users.

    I understand Apple’s disabling these items from searches, but why force us to go to TinkerTool or other 3rd party solutions when it would be so easy to add a checkbox for advanced searches.

    Dave Marsh had this to say on Feb 20, 2007 Posts: 44
  • I just wish spotlight would recognize eps files, which I use routinely with clipping paths in layout and design, as the image files that they are.

    destructo had this to say on Feb 20, 2007 Posts: 11
  • My major complaint with Spotlight is that it skips invisible files and app/system components which I occasionally need to find to clean up an uninstall or misbehaving application.

    A better solution would be an actual uninstall utility for OS X.  The lack of this feature is absolutely ridiculous.

    Beeblebrox had this to say on Feb 20, 2007 Posts: 2220
  • destructo, there is a way to add file types to Spotlight. I know I’ve added the PHP type in the past.

    If you can’t turn up a utility on MacUpdate, try MacOSXHints.

    Chris Howard had this to say on Feb 21, 2007 Posts: 1209
  • I quite like Spotlight. Not that I think it’s a great interface; it isn’t - in Wil Shipley’s words “it wasn’t designed, it just… happened”. But I use it more and more. At first I hardly used it at all, and found it too slow. Recently I realised if you use it often it’s faster, because some important thing stays in memory.

    Still, Quicksilver all the way.

    Benji had this to say on Feb 21, 2007 Posts: 927
  • Basically, I hope they improve that bloddy menu. And I’d like the option to wait to start searching until you press enter. Or improve the speed of searching while editing the search string, which I’m sure could be done drastically as it is currently appalling. I am stuck on a pre-industrial revolution G4 powerbook, admittedly.

    Benji had this to say on Feb 21, 2007 Posts: 927
  • Page 1 of 1 pages
You need log in, or register, in order to comment