3

Is it possible to change the name of the destination file (add some timestamp as a prefix) when copy is done by rsync ?

For example, I can use the following command to change the name of the destination file using suffix:

rsync --append  -arzvv --backup --suffix='_2015_09_10' /path/to/src/file /path/to/dst

But I would like to attach the time stamp as a prefix, so in the destination we will have some thing like: 2015_09_10_file instead of file_2015_09_10.

  • 1
    Doubt it -- how would rsync know which files it had copied? – Jeff Schaller Oct 13 '15 at 1:01
  • Could you add an example of what you have in mind? – Faheem Mitha Oct 13 '15 at 2:06
  • 3
    Your --suffix is applied to the backup copy of an existing file, not to the new file, which cannot be renamed. You will have to do the rename after the rsync with a separate command. An alternative is to use a destination directory which is the date, so the file keeps the same name. – meuh Oct 13 '15 at 6:12
1

Rsync cannot do this at the level of individual files, but has some support for doing whole trees. E.g.:

/path/to/dst-2019-07-31/file
/path/to/dst-2019-07-30/file
/path/to/dst-2019-07-29/file
...

There are three options:

  • --compare-dest=DIR where only new files are created in the new directory. This is probably not what you want because you'd have to list all the previous version directories.
  • --copy-dest=DIR where unchanged files are copied from DIR to the destination rather than transferring them from the source.
  • --link-dest=DIR is the same, but uses much less disk space on the destination because it does not duplicate the file contents.

You use it like this:

rsync -a --link-dest=/path/to/dst-$YESTERDAY /path/to/src /path/to/dst-$TODAY
0

Actually you can, but kind of hacky.. and only as suffix.

As you have read in the comments, --suffix is for backups of overwritten files. So you rsync the same files from source to destination, kind of forcing rsync to overwrite (-I) - but only if the source file is (-u) newer (which it never is) and using the -b, --backup option and define a backup directory and a suffix.

rsync -Iu --backup --suffix='_2015_09_10' --backup-dir=/path/to/dst /path/to/src/file /path/to/src/file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.