Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 7 down vote accepted

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 /home/desgua/Download/ inside my ~/Desktop folder and then copy test.txt.

share|improve this answer

You can do this with the following command

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

cp doesn't have an option which can do this trick.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.