Search code examples
bashunixrsynccp

BASH: How to copy a file without overwriting older files


I'd like to know how to copy a file without overwriting an older file, I do not have -n switch available.

Both the cp -u and rsync will overwrite older files as far as I'm aware and I do not want this behaviour, I'd like to always keep the older files.

Thanks


Solution

  • You can use the "--interactive" option (present in my cp from GNU coreutils 6.12) to prompt you before overwriting and fill it with a default:

    echo 'n' | cp --interactive ~/somefile.txt ~/somefile_already_exists.txt