2

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'

Errors

FILE: -c: line 0: syntax error near unexpected token `newline'
FILE: -c: line 0: `for'
FILE: Undefined variable
  • What error messages do you get? What shell does the server use? – dessert Oct 11 '17 at 17:08
  • it uses "tcsh" it says: "for: Command not found. FILE: Undefined variable." – april Oct 11 '17 at 17:13
  • if you connect through ssh and run the same command, does it work? – M4rty Oct 11 '17 at 17:16
  • @M4rty No difference – april Oct 11 '17 at 17:42
2

Since the default shell is tcsh on the remote host, you may want to explicitly start a sh shell:

ssh username@servername sh -c 'for FILE in pathToDirectory/*.txt; do grep -q "word1" "$FILE" && grep -q "word2" "$FILE" && echo "$FILE"; done'

Note also that you should double quote the $FILE variable expansion to cope with filenames that have exotic characters in them.

Also note that although you use word1 and word2 with grep, these are regular expressions and not words. To look for words in a file, use grep -wF "word".


It seems as if ssh will always run the login shell to execute the given command on the remote host, and if this is tcsh it is really difficult to get the quoting right in the command line script.

Two solutions (and I'm not fond of either):

  1. Change the login shell on the remote host to a sh-like shell like bash, ksh, or zsh. Do this by logging in on the host and run chsh.

  2. Put the script in a script file and execute it through ssh with

    ssh username@servername sh ./script.sh
    
  • Thanks I did the above as you mentioned , still working on the errors I get: "FILE: -c: line 0: syntax error near unexpected token newline' FILE: -c: line 0: for' FILE: Undefined variable." – april Oct 11 '17 at 17:41
  • @april Huh? What Unix is the machine you're connecting to running? – Kusalananda Oct 11 '17 at 17:56
  • @april Do quote the word1 and word2 strings too. If the words contain special characters, you will have problems. – Kusalananda Oct 11 '17 at 17:58
  • Linux x86_64 GNU/Linux – april Oct 11 '17 at 17:59
  • 1
    yes thanks it worked! As you said I just put the shell code in a script file and run it, thanks again – april Oct 11 '17 at 19:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.