2

I'm using scp to move files between servers on my network as follows...

scp /folder/file user@<ip address>/User/Documents/Transfer

...however I don't want to overwrite duplicate files. Is there any way of renaming the files who's name is already taken in the destination folder?

E.g. If file.txt already exists in the destination folder, the new file1.txt would be renamed to file1-1.txt and subsequent versions would be file1-2.txt etc. In the same way that other OS offer an option to keep both files.

The format of the file names is not strict, I just don't want to have to manually rename files before transfer and I don't want to lose important data.

3

I believe that you will not be able to do this with scp alone. Your best bet would be to use rsync over ssh with something like the following additional options:

--backup --suffix=_`date +"%m%d%Y_%H%M"` --backup-dir=DIR

Some relevant sections of the man pages for rsync:

-b, --backup
   With this option, preexisting destination files are renamed as  each  file  is
   transferred  or  deleted.  You can control where the backup file goes and what
   (if any) suffix gets appended using the --backup-dir and --suffix options.

--suffix=SUFFIX
    This option allows you to override the default backup  suffix  used  with  the
    --backup  (-b) option. The default suffix is a ~ if no --backup-dir was speci-
    fied, otherwise it is an empty string.

--backup-dir=DIR
     In combination with the --backup option, this tells rsync to store all backups
     in the specified directory on the receiving side.  This can be used for incre-
     mental backups.  You can additionally specify a backup suffix using the --suf-
     fix option (otherwise the files backed up in the specified directory will keep
     their original filenames).

This should be able to be massaged to fit your needs. Below is a basic test, you will doubtless have other options including ssh, to add in:

rsync -av --backup --suffix=_`date +"%m%d%Y_%H%M"` --backup-dir=backup test1/ test2/

So with this in place:

  1. A file is transferred from test1 to test2
  2. If this file already exists on test2, but the transferred file is newer, the 'remote' file is updated, and importantly:
  3. The original file on test2 is backed up to test2/backup with an incremental date and time suffix added

Pretty cool? And I suspect exactly what you are looking for. Note that I have used a relative path for the backup directory, an absolute path can also be used...

  • Ace! This will do for me. Cheers – spcurtis81 Oct 3 '16 at 18:13

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.