Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scp or sftp copy multiple files with single command

Tags:

scp

sftp

I'd like to copy files from/to remote server in different directories. For example, I want to run these 4 commands at once.

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

What is the easiest way to do that?

like image 824
user987654 Avatar asked Jun 02 '13 18:06

user987654


People also ask

How do I Sftp multiple files at once?

Getting Multiple Files To download more than one file from the sftp server use the mget command. mget works by expanding each filename listed and running a get command on each file. The files are copied into the local working directory, which can be changed with the lcd command.

Can we SCP multiple files?

Yes, you should be able to do that with -r option to scp .

How do I copy multiple files in one command?

Copy Multiple Files Using CP Command You must provide both the file name and the destination directory when using the cp command to copy multiple files. First, open the specific directory in the terminal and execute the tree command. If you don't know about the tree command, then please check out this blog.

Is SCP and SFTP the same?

The main difference between SCP and SFTP is that SCP is a protocol that allows transferring files securely from a local host to a remote host while SFTP is a protocol that allows file accessing, transferring, and managing over a reliable data stream which is faster than SCP.


4 Answers

Copy multiple files from remote to local:

$ scp [email protected]:/some/remote/directory/\{a,b,c\} ./

Copy multiple files from local to remote:

$ scp foo.txt bar.txt [email protected]:~
$ scp {foo,bar}.txt [email protected]:~
$ scp *.txt [email protected]:~

Copy multiple files from remote to remote:

$ scp [email protected]:/some/remote/directory/foobar.txt \
[email protected]:/some/remote/directory/

Source: http://www.hypexr.org/linux_scp_help.php

like image 117
ios.id0 Avatar answered Oct 19 '22 19:10

ios.id0


From local to server:

scp file1.txt file2.sh [email protected]:~/pathtoupload

From server to local:

scp -T [email protected]:"file1.txt file2.txt" "~/yourpathtocopy"

like image 20
Gtr_py Avatar answered Oct 19 '22 18:10

Gtr_py


You can copy whole directories with using -r switch so if you can isolate your files into own directory, you can copy everything at once.

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

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

so for instance

scp -r [email protected]:/var/log ~/backup-logs

Or if there is just few of them, you can use:

scp 1.txt 2.txt 3.log user@remote-server:upload-path
like image 93
Jiri Kremser Avatar answered Oct 19 '22 18:10

Jiri Kremser


As Jiri mentioned, you can use scp -r user@host:/some/remote/path /some/local/path to copy files recursively. This assumes that there's a single directory containing all of the files you want to transfer (and nothing else).

However, SFTP provides an alternative if you want to transfer files from multiple different directories, and the destinations are not identical:

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

This uses the "here doc" syntax to define a sequence of SFTP input commands. As an alternative, you could put the SFTP commands into a text file and execute sftp user@host -b batchFile.txt

like image 69
alev Avatar answered Oct 19 '22 19:10

alev