Home » Questions » Computers [ Ask a new question ]

GUI grep on OS X

GUI grep on OS X

This is a question coming from a former/current Windows user. If I'm looking at a Finder window in a particular directory, how can I search for files with names matching a particular pattern (e.g. "p*.php") that contain a particular string of text?

Asked by: Guest | Views: 25
Total answers/comments: 2
Guest [Entry]

"You can't do this from the spotlight icon in the menu bar. But you can do it with spotlight:

Navigate to the folder in the finder.
Type your search of the content in the file you are searching for in the search bar on the top right of the folder.
There is a line above the results that says: Search: This Mac ""Your Folder Name"" Click on the name of your folder to restrict the search to the folder instead of the whole computer, which is what the default selection ""This Mac"" does.
Then click the gear icon, choose show search criteria, and narrow down your search. For example you can add ""Name ends with .php"" and then click the plus to add another criteria. You could then add ""Name begins with p""

Edit: I just grabbed a php file and searched for this string array_push($songs,$song) and it will not find the file, but if I only search up to the comma (ie array_push($songs ) spotlight does find the file!

I found this article which explains how to add php as a supported file type for spotlight which should fix the weird result I experienced above. I believe without this change spotlight treats the file as rtf only so some php elements (punctuation especially) will make the search fail. The command to edit the required file is:

sudo nano /System/Library/Spotlight/RichText.mdimporter/Contents/Info.plist

You'll want to reindex after this change as the article mentions. It also mentions the more proper way to do this is to install the developer tools.

You can then use the method I described above to restrict your search to a specific folder or file type."
Guest [Entry]

Some programmer's text editors offer a grep-like feature for searching for a regular expression across multiple files; many (many) moons ago BBEdit had this feature (and probably still does; I just haven't used it for years). I wouldn't be surprised to find that other tools targeted at the developer market also have similar features.