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
install -D file -m 644 -t /path/to/copy/file/to/is/very/deep/there
Simply add the following in your .bashrc, tweak if you need. Works in Ubuntu.
E.g If you want to copy 'test' file to destination directory 'd' Use,
mkcp will first check if destination directory exists or not, if not then make it and copy source file/directory.
Let's say you are doing something like
where A/B/C/D are directories which do not exist yet
A possible solution is as follows
hope that helps!
If both of the following are true:
cp
(and not, for instance, the Mac version), andthen you can do this with the
--parents
flag ofcp
. From the info page (viewable at http://www.gnu.org/software/coreutils/manual/html_node/cp-invocation.html#cp-invocation or withinfo cp
orman cp
):Example:
Such an old question, but maybe I can propose an alternative solution.
You can use the
install
programme to copy your file and create the destination path "on the fly".There are some aspects to take in consideration, though:
You can easily amend the #2 by adding the
-m
option to set permissions on the destination file (example:-m 664
will create the destination file with permissionsrw-rw-r--
, just like creating a new file withtouch
).And here it is the shameless link to the answer I was inspired by =)
I wrote a support script for cp, called CP (note capital letters) that's intended to do exactly this. Script will check for errors in the path you've put in (except the last one which is the destination) and if all is well, it will do an mkdir -p step to create the destination path before starting the copy. At this point the regular cp utility takes over and any switches you use with CP (like -r, -p, -rpL gets piped directly to cp). Before you use my script, there are a few things you need to understand.
CP doesn't have the luxury of taking cues from existing paths, so it has to have some very firm behavior patterns. CP assumes that the item you're copying is being dropped in the destination path and is not the destination itself (aka, a renamed copy of the source file/folder). Meaning:
This default CP behavior can be changed with the "--rename" switch. In this case, it's assumed that
A few closing notes: Like with cp, CP can copy multiple items at a time with the last path being listed assumed to be the destination. It can also handle paths with spaces as long as you use quotation marks.
CP will check the paths you put in and make sure they exist before doing the copy. In strict mode (available through --strict switch), all files/folders being copied must exist or no copy takes place. In relaxed mode (--relaxed), copy will continue if at least one of the items you listed exists. Relaxed mode is the default, you can change the mode temporarily via the switches or permanently by setting the variable easy_going at the beginning of the script.
Here's how to install it:
In a non-root terminal, do:
In gedit, paste CP utility and save: