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
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
cp
has multiple usages:@AndyRoss's answer works for the
style of
cp
, but does the wrong thing if you use thestyle of
cp
.I think that "DEST" is ambiguous without a trailing slash in this usage (i.e. where the target directory doesn't yet exist), which is perhaps why
cp
has never added an option for this.So here's my version of this function which enforces a trailing slash on the dest dir:
Just to resume and give a complete working solution, in one line. Be careful if you want to rename your file, you should include a way to provide a clean dir path to mkdir. $fdst can be file or dir. Next code should work in any case.
or bash specific
This might work, if you have the right kind of
rsync
.Copy from source to an non existing path
NOTE: this command copies all the files
cp –r
for copying all folders and its content$_
work as destination which is created in last commandShell function that does what you want, calling it a "bury" copy because it digs a hole for the file to live in:
(there's no such option for
cp
).