SFTP:将多个文件从一个文件夹移动(重命名)到另一个文件夹(而不是逐个移动)

SFTP:将多个文件从一个文件夹移动(重命名)到另一个文件夹(而不是逐个移动)

我需要将所有文件从当前路径当前路径/目标文件夹

我使用的 SFTP 版本是:SFTP 协议版本 2

可用的命令有:

sftp> help
Available commands:
cd path                       Change remote directory to 'path'
lcd path                      Change local directory to 'path'
chgrp grp path                Change group of file 'path' to 'grp'
chmod mode path               Change permissions of file 'path' to 'mode' 
chown own path                Change owner of file 'path' to 'own'
help                          Display this help text
get remote-path [local-path]  Download file
lls [ls-options [path]]       Display local directory listing
ln oldpath newpath            Symlink remote file
lmkdir path                   Create local directory
lpwd                          Print local working directory
ls [path]                     Display remote directory listing
lumask umask                  Set local umask to 'umask'
mkdir path                    Create remote directory
put local-path [remote-path]  Upload file
pwd                           Display remote working directory
exit                          Quit sftp
quit                          Quit sftp
rename oldpath newpath        Rename remote file
rmdir path                    Remove remote directory
rm path                       Delete remote file
symlink oldpath newpath       Symlink remote file
version                       Show SFTP version
!command                      Execute 'command' in local shell
!                             Escape to local shell
?                             Synonym for help

我没有动量命令。我试过了RNFT并没有起作用。

现在,我可以使用重命名:

 rename current_path/myFile.txt current_path/DestinationFolder/myFile.txt

没关系。但我需要移动全部(或者许多) 文件。以下操作无效:

 rename current_path/* current_path/DestinationFolder/

Couldn't rename file "current_path/*" to "current_path/DestinationFolder/": Bad message

无法通过SSH所以我不能做像 echo "ssh login@server mv * current_path/DestinationFolder/" 这样的事情

我不应该搞乱这个服务器,这是:没有脚本,没有活动等。我非常有限。

你能推荐我一个解决这个问题的方法吗???

笔记:这是在 SOLARIS 中。

答案1

OpenSSHsftp不是执行此类任务的强大客户端。您必须运行两次。第一次收集文件列表,使用该列表生成命令列表,然后在第二次运行中执行这些命令。

像这样:

files=`sftp -b - [email protected] <<EOF
cd /source/folder
ls
EOF`
files=`echo $files|sed "s/.*sftp> ls//"` 

(
  echo cd /source/folder
  for file in $files; do
    echo get $file
    echo rename $file /destination/folder/$file
  done
) | sftp -b - [email protected]

在生产文件上运行脚本之前,我建议您先将生成的命令列表输出到文件中以检查结果是否符合预期。

只需将最后一行替换为:

) > commands.txt

答案2

我同意 cjc 的观点。如果你真的被限制到上述情况,那么允许你下载文件的命令就是输入 local-path [remote-path] 并将其 scp 回来。很好的链接这里也。

我会用类似的东西韓國但事情却变得简单多了。

sftp> help put
USAGE: put local-path [remote-path] [-bg | -fg] [-s] [-o] [-r] [-b | -lf]
DESCRIPTION: Upload file.
PARAMETERS:
 -bg   Start (queue) transfer in background.
 -fg   Start transfer in foreground.
 -s    Include subdirectories (recursive).
 -r    Force existing incomplete file to be resumed.
 -o    Force existing file to be overwritten.
 -b    Upload all files as binary; no conversions.
 -lf   Use auto detection upload mode. Text files are uploaded
       in Unix format, with LF as the line delimiter.

NOTES:
 -     If both '-r' and '-o' are specified, resume is tried first,
       and if that fails, overwrite is used.
 -     '-std' and '-t' transfer mode options are also available
       when SFTP version 4 or higher is in use.

相关内容