如何传输数据:排除100MB以上文件但保留指定扩展名大文件?
Solution for rsync: Exclude large files except specific extensions
Got it, let's break down how to solve this rsync problem. You’re absolutely right that --max-size=100M alone isn’t flexible enough—it enforces a hard limit without exceptions. But rsync’s include/exclude rule hierarchy lets us build exactly the behavior you want: exclude files over 100MB, except for specific file types that can be any size.
The core idea
Rsync processes rules top-to-bottom, and the first matching rule applies. So we’ll structure our rules to:
- First explicitly include the file extensions we want to keep (regardless of size)
- Then exclude all unincluded files that are over 100MB
- Finally include all remaining files (those under 100MB, regardless of type)
Working command example
Let’s say you want to keep .zip, .iso, and .tar.gz files even if they’re over 100MB. Here’s the command:
rsync -av \ --include='*.zip' \ --include='*.iso' \ --include='*.tar.gz' \ --exclude='*' --max-size=100M \ --include='*' \ /path/to/your/source/ /path/to/your/destination/
Let’s break down each part:
-av: Enables archive mode (preserves permissions, timestamps, symlinks, etc.) and verbose output so you can see what’s being synced.--include='*.ext': These lines tell rsync to always sync files with these extensions, no matter how big they are. Add as many of these as you need for your target file types.--exclude='*' --max-size=100M: This combo works together to exclude any file that wasn’t matched by the earlier includes AND is over 100MB. The--max-sizefilters the--exclude='*'rule only to large files.--include='*': Finally, we include all other files that are under 100MB—this covers every file type not in our special list that fits the size limit.
Pro tips for testing and edge cases:
- Test first with dry run: Add
-n(or--dry-run) to the command to see exactly which files would be synced without making any changes. This is critical to avoid accidental data loss or unwanted transfers:rsync -avn \ --include='*.zip' \ --include='*.iso' \ --exclude='*' --max-size=100M \ --include='*' \ source/ destination/ - Handle case sensitivity: If you have files with uppercase extensions (like
.ZIP), add--ignore-caseto make the include rules match regardless of capitalization. - Double-check path slashes: Make sure your source path ends with a
/if you want to sync the contents of the directory, not the directory itself.
内容的提问来源于stack exchange,提问作者codaamok
相关产品推荐
相关产品推荐

