2015-02-08 37 views
0

我正试图在远程服务器上找到包含特定文本的文件。如果有多个文件,它应该列出包含该文本的所有文件。使用shell脚本在远程计算机上查找带有特定文本的文件

下面是我已经能够做出的声明,但不幸的是我在执行时收到错误消息。请指教。

命令:

sshpass -p abc123 ssh [email protected] find /root/Downloads/test/app/beta/System* -type f -mmin -60 | xargs grep -E "put" 

错误消息:

grep: /root/Downloads/test/app/beta/SystemOut_qqq: No such file or directory 
grep: /root/Downloads/test/app/beta/SystemOut_qwp: No such file or directory 

回答

1

通配符正在本地展开。您需要引用它以将其传递给远程主机。

此外,为了使grep能够检查这些文件,它也需要在远程主机上运行,​​因此我们引用了整个命令行。

根据您所述的要求,我添加了一个-l选项来列出包含匹配的文件,而不是打印每个匹配。由于您的示例表达式是静态字符串,因此我从grep -E更改为grep -F。如果您想搜索正则表达式,则可能需要撤消该更改。

sshpass -p abc123 ssh [email protected] 'find /root/Downloads/test/app/beta/System* -type f -mmin -60 | xargs grep -F -l "put"' 
+0

很抱歉,但我得到了同样的错误了。 grep:/ root/Downloads/test/app/beta/SystemOut_qqq:没有这样的文件或目录 grep:/ root/Downloads/test/app/beta/SystemOut_qwp:没有这样的文件或目录 – 2015-02-08 12:56:51

+0

该命令正​​在查找新修改了两个文件,但只有“SystemOut_qqq”中包含“put”文本。但是我得到的输出是“没有这样的文件或目录”以及上面的文件名。 – 2015-02-08 12:58:24

+0

抱歉,粗心编辑导致错误引用,尽管我有自己的指示。现在试试。 – tripleee 2015-02-08 13:42:37

1

试着这么做:

sshpass -p abc123 ssh [email protected] find /root/Downloads/test/app/beta/System -type f -mmin -60 -exec grep -E "put" {} \; 
+0

GOT和出错信息的发现:缺少参数'-exec” – 2015-02-08 12:53:32

相关问题