I want to search for the files in a directory which contain both words wrod1 and word2. I use the following when I want to search in the local directory and it works fine:
for FILE in pathToDirectory/*.txt; do grep -q word1 $FILE && grep -q word2 $FILE && echo $FILE; done
Since I cannot use variables when working with remote host what should I do instead. I found out from other threads(Remote for-loop over SSH) that people use the command for connecting to server and then put the find command in the quote right after. Similar to this:
ssh -l username servername 'for FILE in pathToDirectory/*.txt; do grep -q word1 $FILE && grep -q word2 $FILE && echo $FILE; done'
but this still doesn't work for me.
The command I am running now(still gives me errors):
ssh username@servername sh -c 'for FILE in /pathToDirectory/*.txt; do grep -q "word1" "$FILE" && grep -q "word2" "$FILE" && echo "$FILE"; done'
FILE: -c: line 0: syntax error near unexpected token `newline' FILE: -c: line 0: `for' FILE: Undefined variable