108

Is there a way to search through all the text files in a folder (and subfolders) for a specific string or bit of text in Mac OS X?

8 Answers 8

36

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

  1. Navigate to the folder in the finder.

  2. Type your search in the search bar on the top right of the folder.

  3. 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 change the kind to text files.

5
  • 1
    unfortunately this seems to do OR not AND, and some symbols are stripped out of queries I think. Commented Apr 20, 2012 at 0:50
  • do you need to index your folder first ?
    – jokoon
    Commented Jun 28, 2014 at 13:30
  • no, as long as the folder is normally indexed by spotlight
    – ridogi
    Commented Jun 30, 2014 at 19:41
  • 5
    @Aram Did you figure out how to do an "AND"? I'm searching for "My cat mittens" and it's finding everything that has "My" or "cat" or "mittens" and NOT my exact phrase. EDIT - add quotes around it. Commented Nov 12, 2015 at 7:04
  • Disappointingly sometimes this misses stuff for me that the command line tools find.
    – kuzzooroo
    Commented Jan 13, 2022 at 6:38
147

If you prefer the command line,

grep "my string of text" -R .

You'll need to be (or get) familiar with grep. Read man grep for more info.

7
  • 4
    In my .profile I created this helper : function findtext() { grep -ri "$1" . }. I use it like this : findtext toto where I want to start to search.
    – Maxence
    Commented Sep 17, 2015 at 15:46
  • 1
    downside is it works very slow. grepping in around 50GB of text is painfully slow. Commented Sep 13, 2017 at 3:51
  • 2
    what does this mean: grep: warning: recursive search of stdin Commented Dec 9, 2017 at 18:49
  • 1
    @SuperUberDuper It means you missed the trailing "." (i.e. you didn't specify a directory)
    – user664303
    Commented Sep 5, 2019 at 20:10
  • 2
    Use lower-case 'r'. Upper-case 'R' will also follow symlinks and make your search take longer Commented Mar 24, 2020 at 23:11
37

Very powerful and fast

mdfind "text to find"

Description: Mac Dev Centre - The Power of mdfind

4
  • 1
    it works weird and sometimes doesn't look in specific files
    – hooke
    Commented Jun 12, 2020 at 20:32
  • @hooke Can confirm that mdfind doesn't find everything. Does anyone know why that is? I had luck finding those contents with the_silver_searcher as mentioned in other answer here – but it seems way more taxing on the CPU upon run as it doesn't rely on a previous indexation.
    – P A N
    Commented Nov 28, 2020 at 12:13
  • @hooke the reason is that mdfind searches using the central metadata store in macos, and you can exclude certain directories from being indexed in that store.
    – ehab
    Commented May 10, 2021 at 10:05
  • You saved me, thank you!
    – Emy
    Commented Jul 8, 2022 at 13:17
7

In the upper right hand corner of your screen: Spotlight

BBEdit supports great search, too, in files and folders.

2
  • BBEdit is now TextWrangler and it continues to include excellent multi-file grep search.
    – Phil
    Commented Nov 27, 2015 at 17:15
  • 1
    BBEdit is just BBEdit, TextWrangler is just TextWrangler. BBEdit is a bit more extended than TextWrangler. TextWrangler is free as in free beer. Commented Feb 10, 2016 at 17:13
7
mdfind 'kMDItemTextContent="*text*"c' -onlyin ~/Notes/

c ignores case. See this answer for an overview of the query format and other attributes.

grep -F what? -l -r ~/Notes --include *.txt

-F searches for fixed strings instead of regex. -l only prints the names of the matching files.

1
  • Very good one, very fast, very solid. mdfind 'kMDItemTextContent="*text*"c' -onlyin ~/Notes/
    – YumYumYum
    Commented Sep 4, 2019 at 10:49
3

Open Finder

enter image description here

Navigate to the folder you want to search if you have one.

enter image description here

Enter the term you want to search in the search bar in the upper right hand corner. You may need to stretch out the window to see it.

enter image description here

After you start typing or press enter you'll see a section below the search box to the left that says,

Search: This Mac "Your Folder" Shared

If you want to search your whole computer click on "This Mac". Otherwise click on the folder name next to it. It may already be selected.

enter image description here

To the right side of those options is a "Save" button with a plus sign next to it.

enter image description here

Click the plus sign. You'll see two drop down lists. In the first one select "Kind". In the second choose "Any" or "Text .

enter image description here

Choosing "Any" may find more matches, while "Text" will find files Mac OS X determines fall under the category "Text".

The number of search results will appear at the footer if the footer is shown.

FYI I've noticed that sometimes it takes time to do a search and sometimes there is no indication Finder is doing anything. I wouldn't wait too long but if you're searching a small folder it should be very quick. If searching your Mac it may take up to a minute or more.

Nota bene: To find an exact phrase enclose it in quotes.

1

I'd suggest you look at File Content Finder on the App Store (disclaimer - I'm its developer). It's an affordable app specifically designed for searching file contents without indexing. It supports text files and other major file formats (pdf, doc(x), xls(x), etc).

Its filtering lets you optimise and refine your search by multiple criteria - file type, creation/modification dates, etc.

Here is a detailed documentation on how it works.

0

For the faster search speed, you can use ag (the silver searcher)

Installation: brew install the_silver_searcher

Usage is alike, just replace the grep to ag

For example,

ag "my string of text" -R .

More information can view on Github.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .