How to Transfer Files in Linux with SCP and Rsync
In this tutorial we will explain how to use SCP (Secure Copy) and Rsync, two commands that can be used to transfer files between two machines.
For example, we can copy a file or directory from local to remote or from remote to local systems.
scp to transfer files, everything is encrypted so sensitive details are not exposed.
In this tutorial, we give examples of how to use
rsync commands to transfer files.
SCP (Secure Copy)
scp copies files between hosts on a network.
It uses ssh(1) for data transfer, and uses the same authentication and provides the same security as ssh(1).
scp command relies on
ssh for data transfer, therefore it requires an ssh key or password to authenticate on the remote systems.
The general syntax and usage of
1 scp [OPTION] [user@]local:]file1 [user@]remote:]file2
scp provides a number of options which are explained in more detail.
Transfer Files from Local to Remote with SCP
To copy or transfer a file from a local machine to a remote machine, run the following command:
1 scp image.png firstname.lastname@example.org:/remote/directory
- image.png is the name of the file we want to transfer from local to remote,
- remote_username is the user on the remote server,
- 10.10.0.1 is the server IP address,
- /remote/directory is the path to the directory we want to copy the file to.
Note: If you don’t specify a remote directory, the file will be copied to the remote user home directory.
When you press enter, you will be prompted to enter the remote user password and the transfer will start.
Omitting the filename from the destination location copies the file with the original name. If you want to save the file under a different name you need to specify a new name:
1 scp image1.png email@example.com:/remote/directory/new_image.png
Transfer Files from Remote to Local with SCP
To transfer a file from a remote machine to your local machine, run the following command:
1 scp firstname.lastname@example.org:/remote/directory/new_image.png /local/directory
Transfer a Directory Recursively from Local to Remote
To transfer a directory and all it’s contents from a local machine to a remote host, use the following command:
1 scp -rp sourcedirectory user@dest:/path
NB: This creates the sourcedirectory inside /path thus the files will be in /path/sourcedirectory
rsync is used to copy files either to or from a remote host, or locally on the current host.
rsync is generally used to transfer large files.
Transfer a File from Local to Remote with Rsync
To copy a file from your local machine to a remote host with
rsynch, run the following command
1 rsync -ave ssh mydirectory email@example.com:/remote/directory/
In this tutorial, you learned how to use the
rsync command to copy files and directories between two machines.