Efficiently Syncing Specific File Lists with rsync: An In-depth Analysis of Command-line Arguments and the --files-from Option

Dec 02, 2025 · Programming · 11 views · 7.8

Keywords: rsync | file synchronization | command-line arguments

Abstract: This paper explores two primary methods for syncing specific file lists using rsync: direct command-line arguments and the --files-from option. By analyzing real-world user issues, it explains the workings, implicit behaviors, and best practices of --files-from. The article compares the pros and cons of both approaches, provides code examples and configuration tips, and helps readers choose the optimal sync strategy based on their needs. Key technical details such as file list formatting, path handling, and performance optimization are discussed, offering practical guidance for system administrators and developers.

Introduction

In file synchronization and backup operations, rsync is a powerful tool widely used in Unix-like systems. Users often need to sync specific file lists rather than entire directory structures, which is common in deployment, backup, and configuration management. This paper is based on a typical scenario: a user attempted to sync about 50 files across subdirectories using the --include-from option but encountered unexpected sync behavior. Through in-depth analysis, we find that the --files-from option provides a more direct and efficient solution.

Problem Background and Initial Attempt

The user initially tried to sync files with the following command:

rsync -avP -e ssh --include-from=deploy/rsync_include.txt --exclude=* ./ root@0.0.0.0:/var/www/ --dry-run

Here, rsync_include.txt contains a newline-separated list of relative file paths. The user observed that without --exclude="*", all files were synced; with it, no files were synced. This reveals the complexity of interactions between --include-from and --exclude, especially under default inclusion behaviors.

Core Solution: The --files-from Option

According to the rsync manual, the --files-from option is designed specifically to specify the exact list of files to transfer. Its workings include:

Example command:

rsync -a --files-from=/tmp/foo /usr remote:/backup

If /tmp/foo contains the string "bin", the /usr/bin directory will be created as /backup/bin on the remote host. If it contains "bin/" (with a trailing slash), the immediate contents of the directory will also be sent.

Direct Command-line Argument Passing

As an alternative, file lists can be passed directly as command-line arguments:

rsync -avP -e ssh `cat deploy/rsync_include.txt` root@0.0.0.0:/var/www/

This method assumes the file list is not too long to exceed command-line length limits and contains only real paths (no comments or regex patterns). Its advantage is simplicity, avoiding potential surprises from the implicit behaviors of --files-from.

Technical Details and Best Practices

When using --files-from, note these key points:

For the command-line argument method, ensure correct file list formatting to prevent parsing errors from special characters. In automation scripts, using --files-from is recommended for better maintainability and readability.

Comparison and Selection Advice

Both methods have their strengths and weaknesses:

In practice, choose based on file count, path complexity, and automation needs. For example, in CI/CD pipelines, --files-from might be more reliable.

Conclusion

By analyzing rsync's --files-from option and command-line argument methods, this paper provides a practical guide for syncing specific file lists. Understanding how these tools work and their limitations helps users avoid common pitfalls and optimize file synchronization. Whether for simple command-line use or complex automation scripts, selecting the right method enhances efficiency and reliability.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.