Nizan Grauer
Nizan Grauer

Reputation: 71

List files on remote server

I'm trying to run the following command:

ssh -A -t -i ~/.ssh/DevKP.pem -o StrictHostKeyChecking=no root@MyServer "for file in  \`ls /root/spark/work/ \`; do  echo 'file - ' $file;  done"

The output is:

file - 
file - 
Connection to MyServer closed.

When I ran the command on the remote server itself:

for file in  `ls /root/spark/work/ `; do  echo 'file - ' $file;  done

I get the output:

file -  test1.txt
file -  test2.txt

How do I get ti to work on the local server? it seems that it gets the right files (because there were two sysouts)

anyone has any idea?

thanks

Upvotes: 0

Views: 3125

Answers (1)

arco444
arco444

Reputation: 22881

You need to escape the $ in $file to make sure the remote shell interprets it instead of your local. You should also simplify the ls /root/.. to for file in /root/../*:

ssh root@MyServer "for file in /root/spark/work/* ; do  echo 'file - ' \$file;  done"

Upvotes: 2

Related Questions