Whether you need to copy files locally or transfer them to a remote server, you can do that with rsync – a file synchronization utility.
The following command installs rsync for CentOS distributions:
sudo yum install rsync
For Debian and Ubuntu:
sudo apt-get install rsync
Copying files locally
To copy the contents of /source/ to /destination/ on the same machine, run:
rsync --archive --verbose --progress /source/ /destination/
By using the --archive option, we are recursively copying the /source/ contents to the /destination/ directory including symlinks, permissions, modification times, owner, group and file mode data. The --progress shows progress during transfer, and --verbose increases verbosity.
If the /destination/ directory does not exist, it will be created. If the /destination/ directory already exists and contains some files that the /source/ directory does not contain, these files will be preserved.
Copying files from a remote system
With rsync, you can rely on SSH for remote data transfer. Before you start, make sure that:
- rsync client is installed on both local and remote machines;
- you can establish an SSH connection to the remote machine using "ssh user@remotehost"command.
To transfer files from the remote server, specify a full path to the remote source directory including username on the remote host, and а full path to the local directory:
rsync --archive --verbose --progress user@remotehost:/remote/source/ /local/destination/
In this example, we are copying the contents of the /source/ directory on the remote host to the /destination/ directory on the local machine, where the rsync command was executed. Depending on the SSH authentication method configured on your machine, you might be prompted to enter SSH password or other credentials upon execution of the rsync command.
Copying files to a remote system
To transfer files to the remote server over SSH, specify the full path to the local source directory and а full path to the remote directory including username on the remote host:
rsync --archive --verbose --progress /local/source/ user@remotehost:/remote/destination/
The trailing slash
When you specify the path to the source directory, pay attention to the trailing slash — the / symbol — at the end of the directory name. The trailing slash means "the contents of". For example, if there is a trailing slash on the /source/, rsync will copy the contents of /source/ to the /destination/. If there is no trailing slash on the /source, rsync will create a directory with the same name at the /destination/ and copy all the contents of the /source/ to the /destination/source/. On the contrary, the trailing slash on the /destination/ does not make any difference.
The rsync deamon
Consider setting up the rsync daemon if you need to copy a larger volume of information over a private network. The useage of the daemon eliminates the overhead of SSH tunneling, yet the data is transfered unencrypted.
Please refer to the rsync documentation for the instructions how to launch rsync in daemon mode, and for details on rsyncd.conf configuration file.