1833852 Members
1898 Online
110063 Solutions
New Discussion

Re: cp command

 
SOLVED
Go to solution
Tom Dau
Occasional Contributor

cp command

Hello,

Is there a way to use the cp command to copy a file with absolute path to another directory that would create the paths for you? For example:
I have this file /u01/scripts/sql/test.sql and would like to copy test.sql to /u02. Since I don't have scripts/sql directory created under /u02, the cp -r command won't create it for me. It simply put /u01/scripts/sql/test.sql to /u02/test.sql. In Linux I can use the switch --parents and that creates the directory structure for me. Any help is appreciated.

thanks,
6 REPLIES 6
Rita C Workman
Honored Contributor
Solution

Re: cp command

Well a single file I haven't got it to do, but I can get it to copy everything under a directory (files and sub-directories) and create those directories:

cp -r /root/testdir1/* /root/testdir2/

Where /testdir2 did NOT exist...
it copies everything over including sub-dir .. /root/testdir2/testdir3/testfile3

But a single file...maybe somebody else can get that one..

Rgrds,
Rita
A. Clay Stephenson
Acclaimed Contributor

Re: cp command

Not directly. You can test to see if a directory exists and then issue a mkdir -p /aaa/bbb/ccc/ddd command which will create any missiing directories in the path.
If it ain't broke, I can fix that.
Tom Dau
Occasional Contributor

Re: cp command

Thank you all. I guess I have to live with that. The reason is if I have many many files with different directory structures, then I have to script it somehow to make it work especially those files come from the find command.

Thanks again.

TD
Jannik
Honored Contributor

Re: cp command

have you tried:
cd /u01
find . -depth | cpio -dumpv /

or the tar version:
cd /u01
tar cf - .|(cd /u02; tar xf -)
jaton
Dennis Handly
Acclaimed Contributor

Re: cp command

Instead of using cp, you can use tar or pax to "copy" the file. This will create any needed directories. pax(1) allows renaming.
Hareesh Kumar
Advisor

Re: cp command

Hi Tom,

You can try with the -R option with cp. but this will copy the entire directory to the new locaion.

The command would be

cp -prR u01/scripts /u02

This will create the directories u02/scripts/sql/test.sql and copy the entire contents of the directory scripts to the new location.
Please ignore if you have already tried this option.