How to copy a file when destination directory does not exists.
Can I copy a file when destination directory does not exist. In below example i want to copy index.html to /home/www/example2 wherw example2 does not exists.
Quote:
|
you can test it easily (no, it won't do it)
and also you can imagine, if example2 did not exist cp cannot decide: is this the target dir or target filename. |
Quote:
Code:
mkdir /home/www/example2 Code:
install -D /home/www/example/index.html /home/www/example2/index.html |
I don't think the cp command can do that, unless I'm missing something in the manual page, though the cp command has some other interesting directory creating functionality (like the --parent command). I think you have to use mkdir -p along with the cp command, or you could create a single perl or python script to do this (which shouldn't be very difficult).
|
Quote:
|
Quote:
|
You could set up a short function like this to include directory creation:
Code:
cp2dir() { mkdir -p "${!#}" && cp "$@" ;} Note too that it will also pass through all other arguments, so the traditional cp options can still be used. But beware that it only works with the traditional cp syntax where the targetdir is the final argument, so never use the "-t/-T" options with it. Similar functions could, of course, be set up for mv or any other command, although I don't know if I'd personally trust it with anything that could potentially destroy the original. |
All times are GMT -5. The time now is 01:12 AM. |