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?
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.
Yes, you should be able to do that with -r option to scp .
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.
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.
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
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"
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With