Question: I need a simple example of how to use cp --no-target-directory.I do experience some difficulties in understanding cp --no-target-directory. I do understand the explanation for mv --no-target-directory, but I can't really imagine a way to use it for cp.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, ifmv -T /tmp/source /tmp/dest succeeds, there is no question that /tmp/source was renamed to/tmp/dest`. (source)
cp --no-target-directory explained
directory;cp;coreutils
By default, cp tests if its last argument is an existing directory. If this happens, cp creates a link inside that directory, with the base name of the source. That is, given the commandcp foo/bar wibbleif wibble is an existing directory then cp copies the source to wibble/bar. If wibble does not exist then cp links the source to wibble.If you want to be sure that the copy is always wibble, then you can specify the --no-target-directory (alias -T) option. That way, if cp succeeds, you can be sure that the copy is called wibble. If wibble already existed as a directory, then cp will fail.In tabular form:The target is             Without -T               With -Texisting directory          copy in the directory    errorexisting file (not dir)     overwrite                overwritedoes not exist              create                   createThe only difference is that with -T, in case the target is an existing directory, the command returns an error. This is useful when you expect the directory not to exist: you get an error message instead of something unpredicted happening.The same applies to mv and ln. If the target is an existing directory, with -T, they signal an error rather than silently doing something different.With cp, there's a different case. If you do a recursive copy and the source is a directory, then cp -T copies the content of the source into the destination, rather than copying the source itself. That is, given$ tree source destination source foodestination barthen$ cp -rv source destination`source' -> `destination/source'`source/foo' -> `destination/source/foo'whereas% cp -rvT source destination`source/foo' -> `destination/foo'