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:
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:
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:
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.
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:
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:
To filter file transfers by file size, you can use the “—max-size” option:
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:
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:
To include and exclude files from the list of items to be copied, you can use the —include and —exclude options:
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:
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