Can you tell cp (GNU coreutils) to prevent reporting an error on:

$ echo 1 > foo
$ cp foo foo

Currently, this reports:

cp: 'foo' and 'foo' are the same file

That's very sensible, but it's not always necessarily going to be an error. Conceptually, if you are copying files from $source to $dest, if you are already in the $dest directory, then essentially the copy is already done by construction; so, depending on context, this should not necessarily be reported as an error. I can't find anything in the manual page for this. The --update option doesn't work although it sounded like it might.

Obviously, I can work around this is many ways, but I was wondering if cp could do this off the bat.


This is not possible with GNU coreutils (as of coreutils 8.27). The documentation doesn't mention it, and the source code doesn't show any undocumented way. The test for same-file-ness only permits it when making hard links or in some cases where the files are symbolic links to the same destination.

Rsync allows it, so a simple solution would be to use rsync instead of cp.

  • Oh, rsync: that's a good shout. Thanks! Thanks for the link too :) – Zorawar Jun 10 '17 at 23:11

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.