cp function not creating folder hierachy

Asked by mrbeardy

Background:
I'm a multi-media designer who knows just enough programming skill to get myself by...

I've hacked together some PHP-CLI code that creates Bash files for me. Not the most elegant way to solve the problem but works well most of the time. In this case it creates a whole bunch of cp commands.

Problem:
My problem is that it won't create a folder hierarchy for files to land in.

Input:
cp -v /home/user/Desktop/existing\ folder/sub\ folder/file.jpg /home/user/Desktop/folder\ to\ create/sub\ folder/file.jpg

Output:
cp: cannot stat `/home/user/Desktop/existing\ folder/sub\ folder//home/user/Desktop/folder\ to\ create/sub\ folder/': No such file or directory

Analysis:
* When I make the folder structure manually the file copies fine
* Sometimes I get 'cp: target' instead of 'cp: cannot stat'

Question:
Is this just the nature of the cp function and you just have to programatically make the directory structure using mkdir before copying or am I using the function wrong? Or something else...

Thanks

Question information

Language:
English Edit question
Status:
Solved
For:
Ubuntu Edit question
Assignee:
No assignee Edit question
Solved by:
Sébastien Corriveau
Solved:
Last query:
Last reply:
Revision history for this message
Best Sébastien Corriveau (sebcor-deactivatedaccount) said :
#1

As you notice, cp won't create the missing directory structure itself. But you can do it easily by issuing a single mkdir command before cp:

mkdir -p /home/user/Desktop/folder\ to\ create/sub\ folder

The "-p" parameter allows you to create multiple levels of subfolders in a single command.

Revision history for this message
mrbeardy (mrbeardy) said :
#2

Thanks Sébastien Corriveau, that solved my question.