When transferring a complete website or a group of files from one from server to another, the scp command can be used perform the transfer when working between Linux servers.
The scp command copies files between hosts on a network and uses the ssh protocol for data transfer, as well as the same authentication as the ssh protocol and provides the same security as ssh.
File names may contain a user and host specification to indicate that the file is to be copied to and/or from a specific hosts. Local file names can be made explicit using absolute or relative pathnames.
When copying a source file to a target file which already exists, scp will replace the contents of the target file. If the target file does not yet exist, an empty file with the target file name is created and then filled with the source file contents.
The following options can be very helpful when using scp:
- r : Recursively copy the files and subdirectories.
- C : Enables compression for the data transfer activities.
- p : Preserves file access, modification times, and modes from the original file.
scp Command Examples
To transfer a specific file from a local directory to specific location on a remote server, with compression and preserving file access, modification times, and modes the following command can be used:
scp -Cp /localpath1/to/file.ext user1@server1:/remotepath1/to/file.ext
To transfer all files and subdirectories from a remote server recursively to a local directory, with compression and preserving file access, modification times, and modes the following command can be used:
scp -rCp user1@server1:/remotepath1/to/dir/ /localpath2/to/dir/
To transfer all files and directories from a remote server recursively to another remote server, with compression and preserving file access, modification times, and modes the following command can be used:
scp -rCp user1@server1:/remotepath1/to/dir/ user2@server2:/remotepath2/to/dir/