If cp's destination path turns out to be a directory, how do I avoid mistakenly creating files inside that directory?

A redirection would do that. It behaves as cp's -T, --no-target-directory (it exits with error if dst is a directory):

$ cat src > dst

In terms of absolute compatibility, you could check for a target directory before attempting the copy.

Either error out,

[ -d dest ] && echo "${0##*/}: is a directory: dest" >&2 && exit 1
cp src dest

or remove the target silently

[ -d dest ] && rm -rf dest
cp src dest

There is no atomic operation available to delete a directory and replace it with a file. (You might find a command such as cp --no-target-directory, but the underlying operation is still fundamentally not atomic. All you're doing is reducing the attack surface, not eliminating it.)

Tags:

Cp

Files