0

I'd like to know if it's possible to move/copy files using the command line and automatically rename conflicting files? I scoured the forums and found different suggestions but none of them worked. So far I've tried:

  1. cp -r --backup=t /path/to/source /path/to/target
  2. cp --backup=numbered /path/to/source /path/to/target
  3. cp -R --backup=existing --suffix=.orig -t /path/to/source /path/to/target

It's a large folder (700GB) which contains a lot of old files. The third command "kinda" worked (on some files), but I noticed that it didn't work on certain other files, since I left the PC on tonight to transfer the files and checked if it had overwritten anything (which it had).

Thank you for your help.

4
  • maybe the solution to this question would be the easiest way to go (based on rsync)? Otherwise, I would suggest a custom shell script that loops through each file in source-dir and checks for a file with that name in target-dir, and adds a counter-suffix to the filename if it already exists...
    – jov14
    May 4, 2021 at 14:33
  • @jov14: rsync -b is worth an answer.
    – harrymc
    May 4, 2021 at 14:56
  • Thank you for your replies. I found cp --archive --backup --suffix="-original" A/* B but this overwrites the file if there already is an -original - is there any way to make it copy files incrementally? Like: example.txt, example.txt.original, example.txt.original.1, .2 and so forth?
    – telometto
    May 4, 2021 at 16:53
  • I asked a friend of mine if cp was able to perform such a task, but he told me that I would need to script loops in order for cp to be able to do so. Thanks for the help everyone.
    – telometto
    May 5, 2021 at 9:24

0

Your Answer

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