简体   繁体   English

通过 ssh 和 xargs 删除文件

[英]remove files via ssh and xargs

I am trying to remove files via ssh using by find and xargs.我正在尝试使用 find 和 xargs 通过 ssh 删除文件。 On the local machine command works correctly.在本地机器命令正常工作。 I am using exception list also.我也在使用例外列表。

I have tried to changes brackets between ' ' and " " but it does not work.我试图更改 ' ' 和 "" 之间的括号,但它不起作用。

save_files=(test1 test2)

On the local machine:在本地机器上:

find / -mindepth 1 | grep -vE "$(IFS=\| && echo "${save_files[*]}")" | xargs rm -rf

via ssh:通过 ssh:

su - user -c "ssh host find / -mindepth 1 | grep -vE '$(IFS=\| && echo "${save_files[*]}")' | xargs rm -rf"

In above ssh command xargs is performing locally.在上面的 ssh 命令 xargs 正在本地执行。 I need xargs on remote machine.我需要远程机器上的 xargs 。 Even I put find command in '' brackets like below:即使我将 find 命令放在 '' 括号中,如下所示:

su - user -c "ssh host 'find / -mindepth 1 | grep -vE '$(IFS=\| && echo "${save_files[*]}")' | xargs rm -rf'"

Can you try this?你能试试这个吗?

su - user -c "ssh host 'save_files=test; find / -mindepth 1 | grep -vE "$save_files" | xargs rm -rf'"

Or after switching as root user try below one?或者在切换为root用户之后尝试以下一个?

ssh host 'save_files=test; find / -mindepth 1 | grep -vE "$save_files" | xargs rm -rf'

The code for executing the command on a remote host:在远程主机上执行命令的代码:

su - user -c "ssh host 'find / -mindepth 1 | grep -vE '$(IFS=\| && echo "${save_files[*]}")' | xargs rm -rf'"

Is using double quote to pass the command to 'su -c', and then single quotes to pass the command to 'ssh' (which contain double quotes).使用双引号将命令传递给“su -c”,然后使用单引号将命令传递给“ssh”(其中包含双引号)。 However, the quoting does not support nesting, so that the quote in '$(IFS=... ... )' actually terminate the quote in 'find'.但是,引用不支持嵌套,因此 '$(IFS=... ... )' 中的引用实际上终止了 'find' 中的引用。

You can execute the same with a 'set -vx' prefix, to see the expansion您可以使用“set -vx”前缀执行相同的操作,以查看扩展

su - owner -c "set -vx ; ssh localhost ...

and you will see你会看到

++ IFS='|'
++ echo 'test1|test2'
+ su - owner -c 'set -vx ; ssh localhost '\''set -x ; find /foo -mindepth 1 | grep -vE '\''test1|test2'\'' | xargs rm -rf'\'''
Password: 
+ ssh localhost 'set -x ; find /foo -mindepth 1 | grep -vE test1'
+ 'test2 | xargs rm -rf'
-su: test2 | xargs rm -rf: command not found
+ find /foo -mindepth 1
+ grep -vE test1
find: ‘/foo’: No such file or directory

Proposed Solution:建议的解决方案:

I was not able to find a way to get three level quoting (very frustrating) Possible solution may be to create a helper script h.sh, which will perform the 'ssh' command, and then invoking the helper script with 'su - user '/path/to/helper.sh'我无法找到获得三级引用的方法(非常令人沮丧)可能的解决方案可能是创建一个帮助脚本 h.sh,它将执行“ssh”命令,然后使用“su - user”调用帮助脚本'/path/to/helper.sh'

Starting another answer, trying again for one-liner, avoiding the intermediate script.开始另一个答案,再次尝试单线,避免中间脚本。

The key problem is the three level quoting, whereas shell support only two level quoting (double quote, single quote).关键问题是三级引用,而 shell 仅支持两级引用(双引号、单引号)。 I believe it might be possible to eliminate the need for the 3rd level of quoting by using backquote in the pattern, eliminating the need to put it into quotes.我相信可以通过在模式中使用反引号来消除对第三级引用的需要,从而消除将其放入引号中的需要。

IFS=\| P="${save_files[*]}"   su - owner -c "ssh localhost 'set -vx ; find /tmp/xyz -mindepth 1 | grep -vE ${P//|/\\|} | xargs rm -rf'"

The solution create the pattern with 'escapes' - see '($P/|/\|}', eliminating the need to quote the pattern. Should work if the save_files does not have magic characters.该解决方案使用“转义”创建模式 - 参见“($P/|/\|}”,无需引用该模式。如果 save_files 没有魔术字符,则应该可以工作。

Notes:笔记:

  1. Notice that I've replaced '/' with /tmp/xyz, to reduce the change the accidental copy/paste will remove important data (already happen on my VM:-( ).请注意,我已将 '/' 替换为 /tmp/xyz,以减少意外复制/粘贴将删除重要数据的更改(已在我的 VM 上发生:-( )。
  2. The 'set -vx' is for debugging & troubleshooting, and can be removed 'set -vx' 用于调试和故障排除,可以删除

First connect your Remote system and run the bash command whatever you need, like find and xargs首先连接您的远程系统并运行您需要的 bash 命令,例如 find 和 xargs

ssh root@MachineB 'bash command'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM