使用 SSH 从远程主机中删除文件

dim*_*mba 6 linux ssh bash shell

我需要使用 SSH 删除远程目录中的所有文件,

目录本身不能被删除,所以@Wes 的回答不是我需要的。如果它是本地目录,我会运行rm -rf dir/*.

小智 16

这很简单:

ssh HOSTNAME rm -rf "/path/to/the/directory/*"
Run Code Online (Sandbox Code Playgroud)


dim*_*mba 7

根据ssh我机器上的人的说法:

If command is specified, it is executed on the remote host instead 
of a login shell.
Run Code Online (Sandbox Code Playgroud)

这意味着 ssh 传递的命令的 shell 扩展不会在远程端完成。因此我们需要“自包含”命令,它不依赖于外壳扩展。

ssh user@remote-machine "find /path/to/directory -type f -exec rm {} \;"
Run Code Online (Sandbox Code Playgroud)

在这里,查找要删除的文件的所有工作都完全由 完成find,无需 shell 的帮助。

一些类似的问题