I want to cp
a file to another directory but that directory doesn't exist yet.
So I would do:
mkdir /new_place
cp the_file /new_place
Can I do this in one?
I imagine something like cp the_file -m /new_place
if "m" stood for "make dir's that don't exist"
Would this be a chance to use scp, rsync or another copying utility?
With
--parents
you can recreate the directories from the source to the destination. For example:Will create the subdirectories
~/Desktop/home/desgua/Downloads
and then copytest.txt
into it; andwill create
~/Desktop/Downloads
.You can do this with the following command
Note: Use of "/" at the end of path:
When using "/" at the end of source, rsync will copy the content of the last folder. When not using "/" at the end of source, rsync will copy the last folder and the content of the folder.
When using "/" at the end of destination, rsync will paste the data inside the last folder. When not using "/" at the end of destination, rsync will create a folder with the last destination folder name and paste the data inside that folder.
desgua's answer is proper and simple way, but what if you need a protable way ? POSIX defines
cp
without--parent
flag, so it's not gonna work across all systems.One option is to write it in Python if it's installed on the system:
This works as so:
You can easily type two commands by using ;. For instance:
and it should work.