1833757 Members
2855 Online
110063 Solutions
New Discussion

Re: need info

 
Reena Dodake
Occasional Contributor

need info

I am facing some problem while copying from source to destination in shell script.
How to recursively search for the files into subdirectories.
Suppose i can copy files from-to location by command
cp "/home/temp/*.dat" "/home/temp10"

And in /home/temp , i have some subdirectories say "/home/temp/test2" and "/home/temp/test3" where i have some more dat files.

So how will i search all the .dat file into subdirectories?
While copying in the destination path i.e. "/home/temp10" i need that folder also should created and files should copy as below:
"/home/temp10/test2/.dat"
"/home/temp10/test3/.dat"
"/home/temp10/.dat"

How can i make directories while copying?


Thanks,
Reena
4 REPLIES 4
Robert-Jan Goossens
Honored Contributor

Re: need info

Hi Reena,

How about.

# find /home/temp -name "*.dat" -xdev | cpio -pcmudv /home/temp10

Regards,
Robert-Jan
Reena Dodake
Occasional Contributor

Re: need info


Thanks Robert-Jan ...
Reena Dodake
Occasional Contributor

Re: need info

cpio command works fine....
But it copies files from the root path.

means suppose i have files in source location
/user/reena/firstfolder/backup/temp/a.dat
/user/reena/firstfolder/backup/temp/b.dat
/user/reena/firstfolder/backup/temp/aaa/bbb/aa.dat
/user/reena/firstfolder/backup/temp/aaa/bbb/bb.dat
/user/reena/firstfolder/backup/temp/ccc/ddd/aa.dat
/user/reena/firstfolder/backup/temp/ccc/ddd/dd.dat

and at destination location
/user/reena/backup/temp/
So, cp command that i am going to execute is

cp "/user/reena/firstfolder/secondfolder/*.dat" "/user/reena/backup/temp"

So i need in destnation location files to copied i.e,
/user/reena/backup/temp/a.dat
/user/reena/backup/temp/b.dat
/user/reena/backup/temp/aaa/bbb/aa.dat
/user/reena/backup/temp/aaa/bbb/bb.dat

How can copy files from source path to destination path with creating same directory structure as source location ?

Or How can i write a script to achieve this?

Regards,
Reena
Robert-Jan Goossens
Honored Contributor

Re: need info

just change directory before you start the find command;

# cd /user/reena/firstfolder/backup/temp
# find . -xdev -name "*.dat" | cpio -pcmudv /user/reena/backup/temp

or a very simple script

#!/usr/bin/ksh

ORGDIR=/user/reena/firstfolder/backup/temp
DESTDIR=/user/reena/backup/temp

cd $ORGDIR
find . -xdev -name "*.dat" | cpio -pcmudv $DESTDIR

Robert-Jan