LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 12-25-2011, 08:35 PM   #1
entz
Member
 
Registered: Mar 2007
Location: Milky Way , Planet Earth!
Distribution: Opensuse
Posts: 453
Blog Entries: 3

Rep: Reputation: 40
cp: Create any directories necessary to open the destination path


https://bdsc.webapps.blackberry.com/...opic/c/cp.html

please see the above link.
this shows from bb sdk how the "-c" option can eventually omit the need to use mkdir when the target dirs aren't existing yet

i'm trying to find a similar feature in gnu version of cp but no avail so far

how can i replicate the same effect without using mkdir?

cheers
 
Old 12-25-2011, 09:05 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 16,653

Rep: Reputation: 2453Reputation: 2453Reputation: 2453Reputation: 2453Reputation: 2453Reputation: 2453Reputation: 2453Reputation: 2453Reputation: 2453Reputation: 2453Reputation: 2453
rsync ?.
 
Old 12-25-2011, 09:06 PM   #3
lwasserm
Member
 
Registered: Mar 2008
Location: Baltimore Md
Distribution: ubuntu
Posts: 184

Rep: Reputation: 41
-R or -r (for "recursive") One or both of these are common options for many unix/linux commands when a recursive action is desired.
 
Old 12-26-2011, 08:29 PM   #4
entz
Member
 
Registered: Mar 2007
Location: Milky Way , Planet Earth!
Distribution: Opensuse
Posts: 453
Blog Entries: 3

Original Poster
Rep: Reputation: 40
Quote:
Originally Posted by syg00 View Post
rsync ?.
No , i don't think so

Quote:
Originally Posted by lwasserm
-R or -r (for "recursive") One or both of these are common options for many unix/linux commands when a recursive action is desired.
actually i already know how to do recursive copying of directory and that is not what i'm asking to be honest.
the point is , regarding copying files to a destination that doesn't yet exist and basically having cp create that directory beforehand as if i was running mkdir prior to the actual copying ...

thanks
 
Old 12-31-2011, 10:23 PM   #5
lwasserm
Member
 
Registered: Mar 2008
Location: Baltimore Md
Distribution: ubuntu
Posts: 184

Rep: Reputation: 41
Not sure what you are asking for. cp -r sourcedir destdir will copy everything in sourcedir including any subdirectories to destdir, and create destdir and any subdirectories as necessary so that the contents of destdir are the same as sourcedir. What is missing from this behavior that you are trying to accomplish?
 
Old 01-05-2012, 11:59 AM   #6
entz
Member
 
Registered: Mar 2007
Location: Milky Way , Planet Earth!
Distribution: Opensuse
Posts: 453
Blog Entries: 3

Original Poster
Rep: Reputation: 40
Quote:
Originally Posted by lwasserm View Post
Not sure what you are asking for. cp -r sourcedir destdir will copy everything in sourcedir including any subdirectories to destdir, and create destdir and any subdirectories as necessary so that the contents of destdir are the same as sourcedir. What is missing from this behavior that you are trying to accomplish?
okay i've tried what you said using coreutils 7.1
here is what is missing :

if you execute something like
Code:
cp -r source existing/non-existing-destdir
then you're fine , however
Code:
cp -r source existing/non-existing/non-existing-destdir
then you've a problem , and that's what's missing
 
Old 01-06-2012, 06:15 PM   #7
lwasserm
Member
 
Registered: Mar 2008
Location: Baltimore Md
Distribution: ubuntu
Posts: 184

Rep: Reputation: 41
I am confused by your examples. Are you actually using a
command line that looks like
Code:
cp -r source existing/non-existing/non-existing-destdir
or is that supposed to represent what you'd like the result to be?

Maybe there is something else going on here. Are any of the subdirectories that fail to get copied actually symlinks?
 
Old 01-06-2012, 08:11 PM   #8
entz
Member
 
Registered: Mar 2007
Location: Milky Way , Planet Earth!
Distribution: Opensuse
Posts: 453
Blog Entries: 3

Original Poster
Rep: Reputation: 40
Quote:
Originally Posted by lwasserm View Post
I am confused by your examples. Are you actually using a
command line that looks like
Code:
cp -r source existing/non-existing/non-existing-destdir
or is that supposed to represent what you'd like the result to be?

Maybe there is something else going on here. Are any of the subdirectories that fail to get copied actually symlinks?
Yes that's the command line i'm using and it fails although it shouldn't , please try it out yourself
to illustrate :
"source" is the source directory to be copied
"existing/non-existing/non-existing-destdir" is the destination path where the first dir does exist while the last 2 do not !
"existing/non-existing/" is also the destination path however this example actually works as opposed to the above , this means that only one non-existing directory can be created ..... i see that as a bug since if one non-existing directory can be created by cp on the fly then why not an infinite number of dirs ?

in other words , i expect that cp creates all the non existing dirs in the provided dest dir argument with the last non-existing dir actually being the copied source dir (with it's contents of course)

besides commmands such as
Code:
mkdir -p existing/non-existing/......many non existing dirs...../non-existing
work without problems as expected

i hope this makes it clear

Last edited by entz; 01-06-2012 at 08:14 PM.
 
Old 01-07-2012, 03:54 AM   #9
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 243Reputation: 243Reputation: 243
It is not a bug, cp requires that the directory where you want to copy files exists, this is same behaviour in other OS (Mac OS, Windows...)

You can write your own cp function, in your .bashrc file for example
Code:
function mycp()
{
    if [[ -z "$1" || -z "$2" ]]; then
        echo "Usage: mycp SOURCE... DIRECTORY"
        return 1;
    fi
    mkdir -p "$2" 
    cp -r "$1" "$2" 
}
then use it with:
Code:
mycp source existing/non-existing/non-existing-destdir
 
1 members found this post helpful.
Old 01-07-2012, 10:04 AM   #10
lwasserm
Member
 
Registered: Mar 2008
Location: Baltimore Md
Distribution: ubuntu
Posts: 184

Rep: Reputation: 41
I see what you want now! I don't think that the cp command can do that but Cedrik's solution looks pretty good. You could even write a function or alias named "cp" to run Cedrik's code if needed, or else just pass the args on to the regular cp command. Then a separate command would not be necessary.

Last edited by lwasserm; 01-07-2012 at 10:08 AM.
 
Old 01-10-2012, 05:12 AM   #11
entz
Member
 
Registered: Mar 2007
Location: Milky Way , Planet Earth!
Distribution: Opensuse
Posts: 453
Blog Entries: 3

Original Poster
Rep: Reputation: 40
okay thanks guys !
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Move a file to some path and create that path when it doesn't exist WhisperiN Linux - Newbie 6 10-09-2010 11:47 AM
get soft link destination path frado Linux - Software 5 05-19-2007 06:05 PM
cp - create all missing dir's in destination lord-fu Programming 3 05-04-2007 03:53 PM
Kmail links open to wrong destination path tigerflag Slackware 2 12-30-2003 10:43 PM
Kmail links open to wrong destination path tigerflag Linux - Software 0 12-28-2003 01:54 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 09:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration