Linux: copy and create destination dir if it does not exist

flybywire picture flybywire · Oct 7, 2009 · Viewed 354.1k times · Source

I want a command (or probably an option to cp) that creates the destination directory if it does not exist.

Example:

cp -? file /path/to/copy/file/to/is/very/deep/there

Answer

Michael Krelin - hacker picture Michael Krelin - hacker · Oct 7, 2009
mkdir -p "$d" && cp file "$d"

(there's no such option for cp).