0

Further to the earlier query posted Using find with sh - command not working

find . -type f -name '*FW*' -exec grep -iEq 'chmod.*archive|archive.*chmod' {} \; -ls

This cmd was not able to find the files when "archive" or "chmod" text is found in multiple lines. However it is able to scan the files , when "chmod" and "archive" text is found in the same line.

So tried with this command , but it doesnt give the desired o/p

1> find . -type f -name '*FW*' -exec grep -iEq 'chmod.*' {} \; -ls -exec grep -iEq '.*archive.*' {} +

Why this cmd doesnt give the desired o/p?

TRied with this command , it gives the result as expected but it fails intermittently with this error "find: 'pcregrep' terminated by signal 11'

2> find . -type f -name '*FW*' -exec pcregrep -qM 'chmod.*(\n|.)*archive.*' {} \; -ls

Why it is failing with the error?

Kusalananda
  • 333,661
  • I am trying to make sense of your question: You said "text is found in multiple lines". Did you mean "text is found on different lines"? – ctrl-alt-delor Apr 09 '20 at 13:30

1 Answers1

1

Your second command in the question is not giving you the correct output because the ordering of the tests that you use with find is important. The command will only output results for files that contain the chmod string because the other grep command is run after -ls (and does not produce output).

You want to find files that contain both the strings chmod and archive.

find . -type f -name '*FW*' \
    -exec grep -Fiq chmod {} \; \
    -exec grep -Fiq archive {} \; -ls

This calls grep -Fiq chmod on all files that has FW in their name, and then grep -Fiq archive on those that the first grep found a match in. If the second grep also found something, the -ls takes effect.

Kusalananda
  • 333,661
  • Finding files that doesn't contain chmod nor archive in the whole file might be as short as using this command: grep -Eirzvl 'chmod|archive'. Of course, find and grep will walk the directory in diferent ways, but that doesn't seem to be an issue here. –  Apr 09 '20 at 19:16
  • @Isaac The task is to find files that contain both of the strings. If you wanted to find files that contain neither, you would use ! -exec grep -Fiq -e 'chmod' -e 'archive' {} \; with find. With grep -v you'd get files with lines that does not contain the pattern, not files that does not contain the pattern (at all). – Kusalananda Apr 09 '20 at 19:46
  • Yes, I just realize that the command as written doesn't work. Back to the drawing board .... –  Apr 09 '20 at 19:59