bash 将多个文件从一个目录移动到远程 sftp 服务器上的另一个目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51136086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 16:57:47  来源:igfitidea点击:

Move multiple file from one directory to another on remote sftp server

linuxbashcommandrenamesftp

提问by nanosoft

I am connecting to my remote sftp using below command:

我正在使用以下命令连接到我的远程 sftp:

sftp user@host

After inputing password next I get sftp prompt i.e.

接下来输入密码后,我得到 sftp 提示,即

sftp>

My job is to move multiplefiles from directory A to directory B. I am able to do this via rename command but only single file at a time. Is there any command/syntax which can move list of files from directory A to directory B. Something like below:

我的工作是将多个文件从目录 A移动到目录 B。我可以通过重命名命令执行此操作,但一次只能执行一个文件。是否有任何命令/语法可以将文件列表从目录 A 移动到目录 B。如下所示:

rename /A/file1 /A/file2 B/

Just to add I have to do it via command line only by using sftp protocol and Not any tool like fileZilla or winscp.

只是补充一下,我只能通过命令行使用 sftp 协议来完成,而不是像 fileZilla 或 winscp 这样的任何工具。

回答by Kenster

You've indicated in comments that you're trying to avoid anything which makes multiple requests to the SFTP server.

您已在评论中指出您试图避免向 SFTP 服务器发出多个请求的任何内容。

The most widely implemented version of the SFTP protocol is Version 3, draft 02. Notably, this is the version implemented by OpenSSH which is the most widely used SFTP server software. That version of the protocol makes no mention of wildcards, and the command to rename a filerenames a single file or directory from an old name to a new name.

SFTP 协议实现最广泛的版本是版本 3,草案 02。值得注意的是,这是使用最广泛的 SFTP 服务器软件 OpenSSH 实现的版本。该版本的协议没有提及通配符,重命名文件命令会将单个文件或目录从旧名称重命名为新名称。

Any client that renames multiple files will have to issue one rename operation per file, possibly preceded by one or more operations to fetch the file names to be renamed. The client could provide the user with a single command to rename multiple files (or a drag-drop option, or whatever), but at the SFTP protocol level, it will necessarily have to issue at least one SFTP request per file.

任何重命名多个文件的客户端都必须对每个文件发出一次重命名操作,之前可能会进行一项或多项操作以获取要重命名的文件名。客户端可以为用户提供重命名多个文件的单个命令(或拖放选项,或其他),但在 SFTP 协议级别,它必须至少为每个文件发出一个 SFTP 请求。

回答by Paul Hodges

Does it have to be sftp?

必须是sftp吗?

You can issue commands as a block script with ssh directly.

您可以直接使用 ssh 将命令作为块脚本发出。

ssh user@host '
    echo "Moving files"
    date
    rename /A/file1 /A/file2 B/
    date
' > logfile 2>&1

回答by Kevin Lemaire

There is no mvcommand using sftp. The only solution is, like you've said, to use rename.

没有mv使用 sftp 的命令。正如您所说,唯一的解决方案是使用rename.



As a workaround in terminal, you can use ftputilin python. It has a rename function:

作为终端中的解决方法,您可以在 python 中使用ftputil。它有一个重命名功能:

rename(source, target)

It renames the source file (or directory) on the FTP server.

它重命名 FTP 服务器上的源文件(或目录)。

That way, you can easily connect to server, list directory, and create a loop to rename the listed files.

这样,您可以轻松连接到服务器,列出目录,并创建一个循环来重命名列出的文件。