# Find a specific function in several notebooks [duplicate]

I searched several questions in this forum about:

How to search a specific function ...

For example, I have a folder with several notebooks. And several of these notebooks, I used, for instance, Sin function.

That is, I intend to find, among these notebooks, examples using Sin function.

I already tried to use the command Filenames for to get anything, but I did not have succeed.

Edit: The link below show that I want:

Good ways to organize and document collections of mathematica notebooks?

Alternatively, if you want to use Mathematica, then you can do something like so--

dir="path/to/directory";

SetDirectory[dir];

fn = FileNames[];

(* select just .nb files, so you don't import and other files, etc. *)

notebooks = StringCases[fn, ___ ~~ ".nb"] // Flatten;

(* select the notebooks that you want *)

Select[
notebooks,
StringMatchQ[
Import[#, "Plaintext"],
___ ~~ "Sin[" ~~ ___] &]

Out[1]= {"Math_Homework_3.nb", "Not_A_Virus.nb"}


You can easily change this to another function by changing the "Sin[" to another function (or any string, really). You can easily change this also to return the actual code that is present in the notebook.

The StringMatchQ isn't particularly fast for me, but isn't prohibitively slow. To run this on a ~5GB dir with a few GB of notebook files, it takes 5 seconds, with most of that being taken up by Import.

I would simply execute

grep -He '\\<Sin\\>' *.nb


in a command shell (with the appropriate working directory). You might be able to do this within Mathematica, but I doubt it would be worth the trouble.