Search for last modified files in the last 2 minutes in your home directory which contain a certain string

You had a good attempt with your own suggestion

find -type d -mmin -2 -ls | grep -Ril "mystring"

This would have identified directories (-type d) that had been modified within the last two minutes rather than files (-type f). Piping the output of -ls to grep would usually have searched the generated file names for mystring. However, in this case the -R flag changes the behaviour of grep and it ignores your list of filenames, searching instead through every file at and below the current directory.

So, let's split the problem into two parts

  1. Search for last modified files in the last 2 minutes in your home directory

    find ~ -type f -mmin -2
    
  2. [Files] which contain a certain String

    grep -Fl 'certain String' {files...}
    

Now you need to put them together. The {} is a placeholder for the filenames generated by the find from step 1, and the trailing + indicates that the {} can be repeated multiple times, i.e. several filenames

    find ~ -type f -mmin -2 -exec grep -Fl 'certain String' {} +

Changing the grep to echo grep will show you what is being run by the find command; this can be a useful debugging technique:

    find ~ -type f -mmin -2 -exec echo grep -Fl 'certain String' {} +

Please consider running man find and man grep to find out what the various options are, such as the -F and -l in grep -Fl, as otherwise you're not learning anything from the exercise you've been set; you're just copying an answer.


In the zsh shell:

grep -l -F -i 'string' ~/**/*(.Dmm-2)

... where ** matches recursively into subdirectories, and where the .D in (.Dmm-2) means "only match regular files (.), but include hidden files (D)", and where mm-2 means "modified within the last two minutes".