Command Line – Copy File and Create Directory Simultaneously

command linecopydirectoryfiles

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?

Best Answer

With --parents you can recreate the directories from the source to the destination. For example:

cp --parents ~/Downloads/test.txt ~/Desktop/

Will create the subdirectories ~/Desktop/home/desgua/Downloads and then copy test.txt into it; and

cp --parents Downloads/test.txt ~/Desktop/

will create ~/Desktop/Downloads.