首页 文章

scp或sftp使用单个命令复制多个文件

提问于
浏览
217

我想将文件从/向远程服务器复制到不同的目录中 . 例如,我想一次运行这4个命令 .

scp remote:A/1.txt local:A/1.txt
scp remote:A/2.txt local:A/2.txt
scp remote:B/1.txt local:B/1.txt
scp remote:C/1.txt local:C/1.txt

最简单的方法是什么?

14 回答

  • 1

    将多个文件从远程复制到本地:

    $ scp your_username@remote.edu:/some/remote/directory/\{a,b,c\} ./
    

    将多个文件从本地复制到远程:

    $ scp foo.txt bar.txt your_username@remotehost.edu:~
    $ scp {foo,bar}.txt your_username@remotehost.edu:~
    $ scp *.txt your_username@remotehost.edu:~
    

    将多个文件从远程复制到远程:

    $ scp your_username@remote1.edu:/some/remote/directory/foobar.txt \
    your_username@remote2.edu:/some/remote/directory/
    

    资料来源:http://www.hypexr.org/linux_scp_help.php

  • 10

    从本地到服务器:

    scp file1.txt file2.sh username@ip.of.server.copyto:~/pathtoupload

    从服务器到本地:

    scp username@ip.of.server.copyfrom:"file1.log file2.log" "~/yourpathtocopy"

  • 52

    您可以使用 -r 开关复制整个目录,这样如果您可以将文件隔离到自己的目录中,您可以一次复制所有内容 .

    scp -r ./dir-with-files user@remote-server:upload-path
    
    scp -r user@remote-server:path-to-dir-with-files download-path
    

    所以举个例子

    scp -r root@192.168.1.100:/var/log ~/backup-logs
    

    或者,如果只有少数,您可以使用:

    scp 1.txt 2.txt 3.log user@remote-server:upload-path
    
  • 1

    正如Jiri所说,您可以使用 scp -r user@host:/some/remote/path /some/local/path 递归复制文件 . 这假设有一个目录包含您要传输的所有文件(没有其他内容) .

    但是,如果要从多个不同的目录传输文件,并且目标不相同,则SFTP提供了另一种选择:

    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
    

    这使用"here doc"语法来定义SFTP输入命令序列 . 作为替代方案,您可以将SFTP命令放入文本文件中并执行 sftp user@host -b batchFile.txt

  • 1

    最简单的方法是

    local$ scp remote:{A/1,A/2,B/3,C/4}.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

  • 11

    {file1,file2,file3} 的答案仅适用于bash(在远程或本地)

    真正的方法是:

    scp user@remote:'/path1/file1 /path2/file2 /path3/file3' /localPath
    
  • 9

    复制多个目录:

    scp -r dir1 dir2 dir3 admin@127.0.0.1:~/
    
  • 1

    Problem :使用单个SCP命令将多个目录从远程服务器复制到本地计算机,并保留远程服务器中的每个目录 .

    Solution :SCP可以轻松完成此任务 . 这解决了在使用具有多个文件夹的SCP时多次输入密码的烦人问题 . 因此,这也节省了大量时间!

    例如

    # 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\}  .
    

    PS:受到这个伟大答案的启发:scp or sftp copy multiple files with single command


    根据评论,这也适用于 Git Bash on Windows

  • 36

    注意:我提前道歉,只回答上述问题的一部分 . 但是,我发现这些命令对我当前的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/

  • 14

    你的命令工作得很完美,但我也希望在将本地发送到远程时更改文件名 . 我写了一个命令: - sshpass -p password scp /path/to/file.txt root @ hostname:/path/newfile.txt

    但它给出了错误/path /newfile.txt:没有找到这样的文件或目录plz在这种情况下帮助我

  • 3
    scp remote:"[A-C]/[12].txt" local:
    
  • 307

    就我而言,我只能使用sftp命令 .
    所以,我不得不使用带有sftp的批处理文件 . 我创建了一个如下的脚本 . 这假设您正在/ tmp目录中工作,并且您希望将文件放在远程系统上的destdir_on_remote_system中 . 这也适用于非交互式登录 . 您需要设置公钥/私钥,这样您无需输入密码即可登录 . 根据需要改变 .

    #!/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
    
  • 2

    在所有文件具有相同扩展名但具有不同后缀(例如日志文件数)的特定情况下,您将使用以下内容:

    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 ....

    干杯,

    家伙

  • 68

    scp使用ssh进行相同身份验证的数据传输,并提供与ssh相同的安全性 .

    这里的最佳实践是实现“SSH密钥和公共密钥认证” . 有了这个,您可以编写脚本而无需担心身份验证 . 就那么简单 .

    WHAT IS SSH-KEYGEN

相关问题