Skip to content

Rsync command on Linux (copy remote files)

The rsync command on Linux is a very versatile tool for copying remote files that offers many options that make it more flexible how to copy files between remote computers. Notably, it offers remote file copying using the SSH protocol, and allows file synchronization, comparing source and destination.

Its most common use is the ability to copy only the different files between the source and destination. Thus, rsync is widely used for file backups and mirroring.

There are two ways for rsync to transfer files: using a remote shell program, such as ssh or rsh, or through the rsync service, directly via TCP.

Examples:

To copy only those files with a pdf extension that do not exist, or that are different, to the server.com in the /home/uira directory. The “-t” option preserves the file modification time:

$ rsync -t *.pdf server.com: /home/uira

To copy all the files from the server.com directory from the /home/www directory recursively to the local directory /data/www, copying the permissions, symbolic links, attributes, and ownership of the files:

$ rsync -avz foo: /home/www /data/www

With the “-a” option, rsync backs up all files and subdirectories recursively, preserving various file attributes, such as file owner, group, modification time, permissions, and symbolic links. The “-v” option shows the copied files, and the “-z” option compresses the files:

To make local copies to local disks with rsync is very simple. It works like the “cp” command, but efficiently:

$ rsync -avh * /backup

The “-h” option shows the numbers in a more humane way.

rsync also allows you to copy files remotely very easily. It must be installed on the local machine and on the remote machine, which must be running OpenSSH.

$ rsync -aVp -e ssh -z * uiraribeiro @servidorbackup: ~/backup

The “-P” option allows rsync to control partial transfers if the connection drops. In this way, if the connection is closed, when you execute the command again, it will resume where it left off.

The “-e ssh” option causes rsync to use ssh as a transfer protocol, creating an encrypted tunnel.

The “-z” option employs the use of the zlib library to compress the data before sending. This can be useful on slow connections. But it’s not necessary if the files are already compressed.

To copy a directory structure without copying the files, you can use the “-f” option:

$ rsync -av -f"+ */” -f” - *” /home/uiraribeiro uiraribeiro @192 .168.15.5: /home/uiraribeiro

This type of copy can be useful for creating a directory structure to start a new project, for example.

To delete files at the destination that are not present in the source, you must use the “—delete “option:

$ rsync -avz --delete /home/uiraribeiro uiraribeiro @192 .168.15.5: /home/uiraribeiro

To filter file transfers by file size, you can use the “—max-size” option:

$ rsync -avz --max-Size='5m' /home/uiraribeiro uiraribeiro @192 .168.15.5: /mp

In order not to copy changed files to the destination, which are different from the source version, the “-u” option is used. It may be useful to not copy environment configuration files (e.g. config files) from development to production:

$ rsync -avzu /home/uiraribeiro/www [email protected]: /var/www/html

To remove the file from the source, once copied, use the “—remove-source-files”. It is useful when making a ZIP or TAR.GZ of the files, specifically for the purpose of copying somewhere, and which can be deleted immediately after copying:

$ rsync --remove-source-files -zvh /home/uiraribeiro/site.tar.gz [email protected]: /var/www/

To include and exclude files from the list of items to be copied, you can use the —include and —exclude options:

$ rsync -avz --include '*.pdf *.doc *.xls' --exclude '*.png *_pt.jpg' /home/uiraribeiro [email protected]: ~/documents

To compare the source and recipient files, showing the differences but without actually making the copy. The “-i” option compares the files, and the “—dry-run” option:

$ rsync -avzi --dry-run /home/uiraribeiro/www [email protected]: /var/www/
Building file list... done
created directory /volumes/TCUIRA/document
>f+++++++ 37 habitos.pdf
>f+++++++ The art of doing twice the work in half the time - Jeff Shuterland.pdf
>f+++++++ tcc/uira.png
sent 2.03M bytes received 407.02K bytes 695.72K bytes/sec
Total size is 17.89G speedup is 7348.33

The letters of the “-i” option mean:

  • d: identify directory
  • f: identify file
  • L: identify links
  • t: timestamp is different
  • s: size is different
  • p: permission is different
  • o : owner is different
  • g: group is different

The remote address must contain the user’s login (uiraribeiro), the server’s address or IP, followed by a colon “:” and the directory on the remote server where the files go. The user on the remote server must have permissions to write to the server.

Learn much more about Linux in our online course. You can register here. If you already have an account, or want to create one, just log in or create your user here.

Did you like it?

Share