Preventing an endless loop with recursive grep

When finding a string in files within a directory, one can use this:

grep -r "searchstring" dir/subdir/ > listofoccurrences.txt

For brevity sake one can enter the intended directory and use this:

grep -r "searchstring" . > listofoccurrences.txt

which as I found out leads to an endless loop, because listofoccurrences.txt also contains the searchstring and will therefore be perpetually augmented with the searchstring.

Are there ways, such as options or directives, one can use to prevent endless loops with recursive greps other than staying away from the directory to search in?

Yeah, simply output to a file outside of that directory:

Or put it in tmp first, then move it after:

---------- Post updated at 11:05 AM ---------- Previous update was at 11:05 AM ----------

Oh, and GNU grep also has an --exclude=PATTERN option.

Thank you for your response, it was the --exclude pattern that I was looking for.