How can I copy a file and create the target directories at the same time?

It's easy using the install program from the coreutils that is typically used for this very purpose by build systems like automake:

install -D /path/to/source /path/to/destination

Note that install creates all parts of the path just like mkdir -p does, see man install. I'm curious why you didn't include why you want to do that. Calling mkdir and cp is very easy.


Try to use such next function for such situation:

copy_wdir() { mkdir -p -- "$(dirname -- "$2")" && cp -- "$1" "$2" ; }

and use it as

copy_wdir aaa/deep/sea/blob.psd bbb/deep/sea/blob.psd

By the way, GNU cp has a --parents option. It's really close to what you want, but not exactly.
It will also create aaa directory that seems you don't need. However you can first cd to aaa and copy like:

cd aaa && cp --parents deep/sea/blob.psd ../bbb/

With standard (POSIX/Unix) commands, you've got:

pax -rws ':^:dest/dir/:' file .