I have an ever growing remote directory of files that I'd like to sort and filter before I rsync them, with the goal of always keeping only the latest N files in my destination directory (i.e. a rotation scheme, more or less). Since rsync doesn't seem to have options for this, I've been using the technique of "inserting an arbitrary remote command", described here:
https://stackoverflow.com/q/950062/787842
with which I came up with this command, that I execute as a cron on the destination host:
rsync -vrzO --delete \ -e ssh <remote_host>:'$(cd <remote_dir> && ls -t $PWD/* | head -n 25)' \ <destination_dir> This works well the first time (i.e. when the dest dir is empty): only the 25 freshest files get copied. But then when the window "slides forward" (i.e. when the arrival of a newer file should push an older one out, locally), the problem is that the --delete option doesn't work as I'd expect. My guess is that since the filtered out remote files still exist (i.e. they're just being temporarily hidden by the inserted '$(..)' command), then rsync simply cannot filter them out locally. Is my understanding correct, and is there a better way?