Search Using Spotlight From Terminal 0

Trick

Share
Search Using Spotlight From Terminal

Hey,

Spotlight is great, it is one of the best tools on your Mac – if not on any OS – to search for files. Today’s tip will show you how you can use spotlight within Terminal. This is useful if you have any scripts that want to take advantage of the search tool within your Mac, or you just to be able to to do. Like many Terminal tips, it can be easily called and used, and has a lot of functions if you delve into it. I am going to show you how you can get the most out of the trick.

Searching Your Mac With Terminal

The Spotlight search function invoked using the mdfind command. Open Terminal location in Applications > Utilities and type something similar to the following. Note that if you want stop the output because it is finding loads of files, you can quit the process using Ctrl+ C.

mdfind name-of-search

This will then stream an output of every file Spotlight finds using your search term.

Searching Directories

The problem with the basic find is that it will search your entire drive. If you want to narrow down the search, you can add an option like so.

mdfind -name-of-search -onlyin /path/to/location

This will only return results in the path specified. Very useful for narrowing down searches.

 Exact Matches

If you want to find exact matches, for your keyword, you can use this next option. I think this is one of the more useful commands as you generally know the name of the file you want to find. For example if you have a file named ‘James’, it will only find matching files, not the billions of other files such as emails that may reference the name ‘James’.

mdfind -name name-of-file

Search For File Type/Kind

Spotlight has a powerful function called kind. For any search term, if you add ‘kind:pdf’ it will only find pdf’s. The kinds name can be pretty much anything, if you have the kind column enabled in Finder, any name that is in this column will be searchable by the kind function. You can use name such as: music, image, application, folder, jpeg, zip and archive to name but a few.

To accomplish this in Terminal simply type the following:

mdfind kind:text name-of-search

This can be supplemented with the -onlyin and -name option as previously described.

Narrow Down The Date

The final options I am going to show you is using the date function. Most of the time you remember that you modified a file within a the previous week, month or year and there is no point in showing result from 10 years ago. Like spotlight the date:week option can be added. The time period can be terms such as: this week, this month, this year, today, yesterday and even tomorrow.

mdfind date:this week name-of-search

Conclusion

Most people will never need to use this command. However, if you have terminal script where a search could be performed, you just like to mess about this command is rather cool. It can give you a lot of results so expect a large amount of output, but with the right options you can narrow down a search.

If you have any improvements or questions about this command, please leave a comment.


If you want to keep up with the latests post from Mac Tricks And Tips I recommend you subscribe to the RSS Feed.

Where To Next?

  • Subscribe To Mac Tricks And Tips