Quick: Remove temporary rsync files after a failed or cancelled sync

Whether you've cancelled a sync or something else caused it to stop or fail, you might end up in a situation where you have your destination directories riddled with temporary rsync files.
While you might be able to rely on rsync's own --delete-during
or --delete-after
options, there might be situations where the destination directory contains additional files that you don't want to delete. If you want to try this to see if it fits your specific use-case, you can try this along with the --dry-run
flag to see what the results might look like.
For cases where you cannot rely on the above, I have a short find
command that seems to do the trick to find only these temporary files. As I'm not too familiar with regular expressions –especially in the context of command line commands– I chose to use the glob format to keep it simple.
To run the find command without actually deleting any files, you can run the following command:
❯ find -type f -iname ".*.*.[a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9]" -ls
What this does is find and list (-ls
) all files that match the format specified. The format is basically any file whose name starts with a period, contains at least three periods in its filename, and must end with .XXXXXX
, where any of these Xes must be a letter.
After ensuring no unwanted files showed up in the results you've seen come by, you can run the same command but this time with the -delete
flag set:
❯ find -type f -iname ".*.*.[a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9][a-z0-9]" -delete
That's it! You can now re-run rsync
if needed.
I hope this helps.