列出远程服务器上的文件

时间:2015-06-25 10:20:09

标签: bash

我试图运行以下命令:

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

输出结果为:

file - 
file - 
Connection to MyServer closed.

当我在远程服务器上运行命令时:

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

我得到了输出:

file -  test1.txt
file -  test2.txt

如何让ti在本地服务器上运行?它似乎得到了正确的文件(因为有两个sysouts)

任何人都有任何想法?

感谢

1 个答案:

答案 0 :(得分:1)

您需要转义$中的$file以确保远程shell解释它而不是本地。您还应该将ls /root/..简化为for file in /root/../*

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