I'm using GNU coreutils

I have a directory foo containing a file file1 and a directory bar containing file2.

If I now do a cp -rT foo bar, bar will contain both files instead of getting overwriten like the manpage might lead you to believe.

I'd like for ´bar´ only to contain the contents of ´foo´ and not get merged.

  • 1
    What part of the manual are you referring to? Is this using GNU cp from coreutils, or some other implementation of the cp command? – Kusalananda Sep 13 '18 at 11:48
  • I'm referring to treat DEST as a normal file. Normal files get overwritten when you copy over them. This is not the case here. – Philippe Sep 13 '18 at 11:58
  • the contents of the files are irrelevant. I'm talking about the contents of the directories – Philippe Sep 13 '18 at 12:15

With GNU cp, the -T option is used to ensure that the source directory will not be put inside the target directory. It will not cause to contents of the target directory to be removed.

The info documentation on this (info '(coreutils)Target directory') says (it's using mv as an example):



Do not treat the last operand specially when it is a directory or a symbolic link to a directory. This can help avoid race conditions in programs that operate in a shared area. For example, when the command mv /tmp/source /tmp/dest succeeds, there is no guarantee that /tmp/source was renamed to /tmp/dest: it could have been renamed to /tmp/dest/source instead, if some other process created /tmp/dest as a directory. However, if mv -T /tmp/source /tmp/dest succeeds, there is no question that /tmp/source was renamed to /tmp/dest.

Note that mv -T source dest will fail if dest is not empty.

To replace the contents of bar with that of foo in your example, use

rm -rf bar
cp -r foo bar


rsync --archive --delete foo/ bar

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.