Linux Commands

How to rsync a list of files?

rsync or remote synchronization is a Linux and Unix-based utility that syncs files and directories efficiently between two devices or hosts. In this synchronization process, one is the local host or the source from which we will sync the files, and the other is the remote host, where all the synchronization will occur. rsync can sync or copy data in a couple of different ways:

  • It uses ssh or rsh for syncing files to or from a remote shell.
  • TCP also helps rsync to sync files through the rsync daemon.

rsync installation:

If you do not have rsync on your system, utilize the below-given command for the rsync installation.

$ sudo apt-get install rsync

rsync is well-known for its delta algorithm deployment, allowing rsync only to copy the modifications made in the source files on the local host and the current files on the remote host.

rsync syntax:

$ rsync Option Source Destination
  • Option” refers to rsync options.
  • Source” is for the source directory.
  • Destination” is for the destination directory.

Listing files using rsync

Method 1: rsync –list-only option

rsync, use the “–list-only” option for listing files instead of copying them. It does not identify the files that will transfer; it only identifies the candidates for being synced. This option is implied with a single source argument and no destination provided. The two primary usages of this option are as follows:

  • Firstly, it is utilized for converting a copy command with its destination into a file-listing command.
  • You can use –list-only option to specify the multiple sources.

Syntax of rsync –list-only option:

$ rsync --list-only username@remotehost:Source

In the below-given example, we will utilize the rsync –list-only option to list the source directory “testdir2” on the remote host.

$ rsync --list-only linuxhint@10.0.2.15:testdir2/

Method 2: rsync –files-from option:

The rsync –file-from option provides you the exact list of the files to transfer or sync. It modifies the default behavior of rsync and makes it easy to transfer only the specified directories and files. In addition, you can utilize the –file-from option for sorting out files list. This action allows rsync to work faster by avoiding revisiting the path elements that are common between the adjacent entries.

Syntax of rsync –files-from option:

rsync options –files-from=:/Specific_Path/file-list hostname:/ /tmp/copy

Options:

  • -a or archive mode: The -a option with –file-from prevents the -r option’s recursions.
  • R or –relative option: The -R option maintains the information related to the specified path in the command.
  • -d or –dirs option: It will force rsync to create directories specified in the list on the receiving end.

Now, let’s see an example demonstrating the –file-from option of the rsync command. Firstly, we will create a file list of the content present inside the “testdir1” directory. This rsync –file-from option will provide you a list of files that can transfer later.

$ sudo rsync -av --files-from=rsyncfilelist . "/testdir1"

Conclusion:

In Linux-based systems, rsync exists as a free software utility. It is used to sync or transfer directories and files from remote to the local system, local to a remote system, or the same system. For file transfer, sometimes, you need to check the files present in the source directory. This article provided you two methods for attaining the list of files present in the source directory, which includes “–file-from” and “–list-only” rsync options.

About the author

Talha Saif Malik

Talha is a contributor at Linux Hint with a vision to bring value and do useful things for the world. He loves to read, write and speak about Linux, Data, Computers and Technology.