7

If I do a find . -mmin -1 I get 'myfile' which was modified in last one minute.

But when I want to search a particular string in that file by doing

grep 'myString' myfile -mmin -1 

I get the error invalid max count

I also tried

find . -name "myfile" -exec grep 'myString' myfile -mmin -5

I get the error find: missing argument to -exec

So my question is How do I grep or cat only the changes within a file which happened in last 5 mins. Now that I know the file name which was modified during that period.

Suggestions? Thanks in adv.

Some Java Guy
  • 4,992
  • 19
  • 71
  • 108
  • I think with this one, you came closest: `find . -name "myfile" -exec grep 'myString' myfile -mmin -5`, however, if you need to execute the grep from find, it must be `find . -mmin -1 -exec grep -H "mystring" {} \;` where the {} are where to insert the filename, and \; terminates the grep command. In this way, find will search for new files and execute grep on each. – Niels Bech Nielsen Sep 17 '21 at 05:56

4 Answers4

9

grep something *

Error:

grep: invalid max count

Verify that you have a file with a leading dash in the name in the current directory. The file name might be taken for an option.

For example:

grep something // okay

touch -- -mmin

**grep something **

grep: invalid max count

Workaround:

**grep -- something **

From man getopt:

Each parameter after a -- parameter is always interpreted as a non-option parameter.

kenorb
  • 155,785
  • 88
  • 678
  • 743
4

Grep doesn't have an mmin argument as far as I can see. It does have a -m argument with a number parameter. grep 'myString' myfile -m3 will stop after 3 lines containing myString. So, the error message means that 'min' in -mmin is not a valid maximum count.

abesto
  • 2,331
  • 16
  • 28
0

I've also encountered this when forgetting to escape a dash in the grep string:

Example:

history | grep '-m yum'
grep: invalid max count

history | grep '\-m yum'
...
sudo ansible workstations -m yum --fork=20 -a 'name=zsh state=latest'
FlakRat
  • 71
  • 1
  • 3
0

Easy answer is to combine grep command and find command into single command:

grep "search-patten" $(find . -mmin -5)

This command will find all matched lines to "search-patten" in all modified files under current directory younger than 5 minutes ago.

grep "search-patten" $(find . -mmin +5)

This command will find all matched lines to "search-patten" in all modified files under current directory older than 5 minutes ago.

Dudi Boy
  • 4,551
  • 1
  • 15
  • 30