2

I'm a newly minted CS major and I have been wondering what the benefits of using grep versus spotlight on mac. I ran a command to search for a specific file because I lost track of which directory it is in, which took a while, but when I did it in spotlight the result was instantaneous. I enjoy using the command line and it makes my life much simpler than the UI directory view on mac, so I would like to learn how to use it to its full potential.

3
  • Are you only concerned about using grep to find files on your filesystem? In which case it's probably not the best tool to use, find would be a lot better. However it's likely taking a while because of all the permission denied errors it has to get when searching the entire filesystem. Not sure if there is an easy way to resolve that without building a search function that ignores those directories. – jesse_b Aug 7 '17 at 20:46
  • Yes I did find a use of the find method, which I ran and it went for a number of seconds without producing a result. I was surprised by the time but I'm guessing it doesn't use an algorithm that ignores directories that have nothing to do with your file. – Bones223 Aug 7 '17 at 21:37
  • Again, I'm pretty new to this stuff, but graph traversal algorithms and complexity interest me. So the fact that it's slower than spotlight at that particular form of searching makes me so interested in how they actually work! – Bones223 Aug 7 '17 at 21:38
1

Quick comparisons

Iterative

grep is usually to search file contents for matching patterns. Think searching file contents. grep will have to manually walk the file system from a given starting point.

I am curious how you were using grep.

find is the standard *nix command to search your file system for a file. Check out the man page, it has lots of ways to use it. But basically, it walks your file system from a given starting point and looks for matches.

Indexed

spotlight is an OSX utility that searches an indexed list of files. spotlight relies on an index and thus is capable of very fast lookups as it is not manually iterating over the file system. Usually a worker thread does that to build the index.

The tradeoff with spotlight is that if the file has not been indexed, then spotlight will not find it, even if it exists. However, if the directory has been indexed, then spotlight has good performance.

Others

If you are needing to search for file contents and are using version control, say on a development project, ag silver_searcher is a much faster alternative to grep. It works within version control scenarios and respects your list of ignored file types to find files containing a pattern

2
  • I think I used a command that had used find along with grep for a regex pattern to match the file I was looking for. Thanks so muchf or this information! – Bones223 Aug 7 '17 at 21:39
  • Find can also grep for regex patterns. You can use it like this: find / -type [f/d] -name [string] – jesse_b Aug 7 '17 at 21:41
1

As a possible solution for you; OS X also has a locate command that utilizes indexing to find files. That is probably your best bet of seeing the same search performance on the cli that you see in spotlight. In order to setup your database you must run the command:

sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.locate.plist

Once your database has been created you can use:

locate <file>

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.