sftp user@host << EOF
get /some/remote/path1/file1 /some/local/path1/file1
get /some/remote/path2/file2 /some/local/path2/file2
get /some/remote/path3/file3 /some/local/path3/file3
EOF
# copies folders t1, t2, t3 from `test` to your local working directory
# note that there shouldn't be any space in between the folder names;
# we also escape the braces.
# please note the dot at the end of the SCP command
~$ cd ~/working/directory
~$ scp -r username@contact.server.de:/work/datasets/images/test/\{t1,t2,t3\} .
#!/bin/bash
cd /tmp
# start script with list of files to transfer
ls -1 fileset1* > batchfile1
ls -1 fileset2* >> batchfile1
sed -i -e 's/^/put /' batchfile1
echo "cd destdir_on_remote_system" > batchfile
cat batchfile1 >> batchfile
rm batchfile1
sftp -b batchfile user@host
14 回答
将多个文件从远程复制到本地:
将多个文件从本地复制到远程:
将多个文件从远程复制到远程:
资料来源:http://www.hypexr.org/linux_scp_help.php
从本地到服务器:
scp file1.txt file2.sh username@ip.of.server.copyto:~/pathtoupload
从服务器到本地:
scp username@ip.of.server.copyfrom:"file1.log file2.log" "~/yourpathtocopy"
您可以使用
-r
开关复制整个目录,这样如果您可以将文件隔离到自己的目录中,您可以一次复制所有内容 .所以举个例子
或者,如果只有少数,您可以使用:
正如Jiri所说,您可以使用
scp -r user@host:/some/remote/path /some/local/path
递归复制文件 . 这假设有一个目录包含您要传输的所有文件(没有其他内容) .但是,如果要从多个不同的目录传输文件,并且目标不相同,则SFTP提供了另一种选择:
这使用"here doc"语法来定义SFTP输入命令序列 . 作为替代方案,您可以将SFTP命令放入文本文件中并执行
sftp user@host -b batchFile.txt
最简单的方法是
所以列表可以包含目录(A,B和C这里是目录;“1.txt”和“2.txt”是这些目录中的文件名) .
虽然它会将所有这四个文件复制到一个本地目录中 - 不确定这是否是你想要的 .
在上面的例子中,你将把远程文件A / 1.txt,A / 2.txt,B / 3.txt和C / 4.txt复制到一个本地目录,文件名为./1.txt, ./2.txt,./3.txt和./4.txt
{file1,file2,file3}
的答案仅适用于bash(在远程或本地)真正的方法是:
复制多个目录:
Problem :使用单个SCP命令将多个目录从远程服务器复制到本地计算机,并保留远程服务器中的每个目录 .
Solution :SCP可以轻松完成此任务 . 这解决了在使用具有多个文件夹的SCP时多次输入密码的烦人问题 . 因此,这也节省了大量时间!
例如
PS:受到这个伟大答案的启发:scp or sftp copy multiple files with single command
根据评论,这也适用于 Git Bash on Windows
注意:我提前道歉,只回答上述问题的一部分 . 但是,我发现这些命令对我当前的unix需求很有用 .
将特定文件从本地计算机上载到远程计算机:
~/Desktop/dump_files$ scp file1.txt file2.txt lab1.cpp etc.ext your-user-id@remotemachine.edu:Folder1/DestinationFolderForFiles/
将整个目录从本地计算机上载到远程计算机:
~$ scp -r Desktop/dump_files your-user-id@remotemachine.edu:Folder1/DestinationFolderForFiles/
将整个目录从远程计算机下载到本地计算机:
~/Desktop$ scp -r your-user-id@remote.host.edu:Public/web/ Desktop/
你的命令工作得很完美,但我也希望在将本地发送到远程时更改文件名 . 我写了一个命令: - sshpass -p password scp /path/to/file.txt root @ hostname:/path/newfile.txt
但它给出了错误/path /newfile.txt:没有找到这样的文件或目录plz在这种情况下帮助我
就我而言,我只能使用sftp命令 .
所以,我不得不使用带有sftp的批处理文件 . 我创建了一个如下的脚本 . 这假设您正在/ tmp目录中工作,并且您希望将文件放在远程系统上的destdir_on_remote_system中 . 这也适用于非交互式登录 . 您需要设置公钥/私钥,这样您无需输入密码即可登录 . 根据需要改变 .
在所有文件具有相同扩展名但具有不同后缀(例如日志文件数)的特定情况下,您将使用以下内容:
scp user_name@ip.of.remote.machine:/some/log/folder/some_log_file.* ./
这将从远程的给定文件夹中复制名为some_log_file的所有文件,即some-log_file.1,some_log_file.2,some_log_file.3 ....
干杯,
家伙
scp使用ssh进行相同身份验证的数据传输,并提供与ssh相同的安全性 .
这里的最佳实践是实现“SSH密钥和公共密钥认证” . 有了这个,您可以编写脚本而无需担心身份验证 . 就那么简单 .
见WHAT IS SSH-KEYGEN