How can I copy a file to a new location, creating the directory as well

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.


You can do this with the following command

# rsync --recursive the_file /path/to/your/dir/that/doesn't/exists/

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.