Practical Uses of Rsync Command in Linux

Rsync is a very popular command-line tool in Linux for synchronizing files and directories between two different locations. Rsync is commonly used for taking backups, file transfers, copying from one location to another, and mirroring.

Rsync uses a delta-transfer algorithm, which means it only transfers the parts of files that have been changed, making it very efficient for oversized or large numbers of files. It also has the ability to resume interrupted transfers and can work over SSH for added security.

Some common use cases for Rsync commands include:

  • Backing up files and directories to a remote server
  • Synchronizing files and directories between multiple computers
  • Mirroring a website or other remote file repository
  • Transferring large files over a network

Rsync command can be used on various Operating systems like Windows, Linux, macOS, etc.

Syncing files or directory with progress flag in Rsync

Base Syntax of Rsync Command

rsync [OPTION]… SRC [SRC]… DEST

Various options that used with Rsync Command:

  •  –verbose, -v            increase verbosity
  • –archive, -a            archive mode is -rlptgoD (no -A,-X,-U,-N,-H)
  • –recursive, -r          recurse into directories
  • –relative, -R           use relative path names
  • –backup, -b             make backups (see –suffix & –backup-dir)
  • –update, -u             skip files that are newer on the receiver
  • –dirs, -d               transfer directories without recursing
  • –dry-run, -n            perform a trial run with no changes made
  • –del                    an alias for –delete-during
  • –progress               show progress during the transfer
  • –itemize-changes, -i    output a change-summary for all updates
  • –help, -h (*)           show this help (* -h is help only on its own)

Install Rsync on Linux System

Debian/Ubuntu

sudo apt install rsync

RHEL/CentOS/Fedora/Oracle/Rocky & AlmaLinux

sudo yum install rsync

Arch Linux

pacman -S rsync

openSUSE

sudo zypper install rsync

Practical Uses of Rsync Command

As we have mentioned above, Rsync is a useful command tool for managing files over the command line. Using Rsync we can take the backup, mirror, and copy files over the terminal.

1. Syncing a local directory to a remote server over SSH:

rsync -avz -e ssh /path/to/local/directory/ [email protected]:/path/to/remote/directory/

This command uses the -a flag for archive mode, -v for verbose output, -z for compression, and -e flag followed by the ssh command to connect to the remote server.

2. Syncing a remote directory to a local machine over SSH:

rsync -avz -e ssh [email protected]:/path/to/remote/directory/ /path/to/local/directory/

This command is similar to the previous one, but it copies files from the remote server to the local machine.

3. Syncing a directory with progress:

rsync -av --progress /path/to/local/directory/ /path/to/remote/directory/

This command uses the –progress flag to display the progress of the transfer.

4. Syncing a directory and exclude certain files:

rsync -av --exclude='*.log' /path/to/local/directory/ /path/to/remote/directory/

This command uses the –exclude flag followed by a pattern to exclude all files with the extension .log from the transfer.

5. Syncing a directory and preserving symbolic links:

rsync -avL /path/to/local/directory/ /path/to/remote/directory/

This command uses the -L flag to preserve symbolic links during the transfer.

6. Syncing a directory and preserving hard links:

rsync -avH /path/to/local/directory/ /path/to/remote/directory/

This command uses the -H flag to preserve hard links during the transfer.

7. Syncing a directory and preserving permissions:

rsync -avp /path/to/local/directory/ /path/to/remote/directory/

This command uses the -p flag to preserve permissions during the transfer.

8. Syncing a directory and preserving timestamps:

rsync -avt /path/to/local/directory/ /path/to/remote/directory/

This command uses the -t flag to preserve timestamps during the transfer.

9. Syncing a directory and deleting files that are no longer in the source:

rsync -av --delete /path/to/local/directory/ /path/to/remote/directory/

This command uses the –delete flag to delete files from the destination directory that are no longer in the source directory.

10. Syncing a directory and creating a backup of files that are overwritten:

rsync -avb /path/to/local/directory/ /path/to/remote/directory/

This command uses the -b flag to create a backup of files that are overwritten during the transfer. The backup files will be suffixed with ~.

Must Read: How to Move Files and Directories using Linux mv Command

Frequently Asked Questions about Rsync

Can Rsync be used to synchronize files between different operating systems?

Yes, Rsync can be used to synchronize files between different operating systems as long as they both have the Rsync command installed.

Can Rsync be used to synchronize files over the internet?

Yes, Rsync can be used to synchronize files over the internet, either by using the rsync protocol directly or by tunneling the connection through SSH.

Can Rsync copy permissions and ownership of files?

Yes, Rsync has the option to preserve permissions and ownership of files when transferring them.

Can Rsync copy symlinks?

Yes, Rsync can copy symlinks, but it also has the option to copy the files that the symlinks point to instead.

Can Rsync be used to create incremental backups?

Yes, Rsync can be used to create incremental backups by using the “–link-dest” option, which creates hard links for files that have not changed since the last backup.

Can Rsync be used to synchronize files with a mobile device?

Yes, Rsync can be used to synchronize files with a mobile device, provided that the device has a Rsync client installed or the device can connect to the server via SSH.

Can Rsync be used to sync files with cloud storage?

Yes, Rsync can be used to sync files with cloud storage services, provided that the service has a way to connect to a server via SSH, or has a Rsync client that can connect to the server.

Can Rsync be used to transfer files to a remote server that is behind a firewall?

Yes, Rsync can be used to transfer files to a remote server that is behind a firewall, provided that the firewall allows incoming connections to the Rsync port (873) or the SSH port (22).

Conclusion

In this article, we have learned, how can use the Rsync commands in a Linux system, it can be used for taking backups, file transfers, copying from one location to another, and mirroring, and it can be performed in local or remote machines using an SSH connection.

So far if you get any questions regarding the practical uses of Rsync commands or related to file management on Linux, do comment, and we will reply as soon as possible.


Pronay Sarkar

Hello!
I am Pronay Sarkar, I love to write about tech and stuff. I am a college graduate from the University of Delhi.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.