Home: Perl Programming Help: Intermediate:
file creation

New User

Jun 16, 2008, 3:27 PM

Views: 5615
file creation

I want to write a perlscript that will traverse through a series of subdirectories and print out a dynamic string to a new file that will be executed separately from perlscript that created it. The perlscript would take 2 arguments: directory name and destination server. So if I passed the following 2 arguments to the perlscript:



It would generate the following in a file that I would execute later.

cp /opt/oracle/oradata01/redo1.dbf

cp /opt/oracle/oradata01/control1.dbf

cp /opt/oracle/oradata02/redo1.dbf

cp /opt/oracle/oradata03/subdir/usertablespace.dbf

Is there a good way to do this with a perlscript so no matter what server I'm on or directory structure I give, it'll start at the top level of the directory I give and traverse through subdirectories and files to construct the cp statements?

Thanks for any help with this.


Jun 16, 2008, 5:03 PM

Views: 5609
Re: [1951g] file creation

If you want to traverse a directory and all of its subdirectories use the File::Find module.


Jun 16, 2008, 10:40 PM

Views: 5604
Re: [1951g] file creation

The cp commands you show are not complete - they only have one argument. How is destServerXYZ part of what you want to do?

Also, you might be able to do this with just the Unix find utility:

find /opt/oracle/oradata -name '*.dbf' -exec echo cp '{}' ... ';'

New User

Jun 17, 2008, 6:47 AM

Views: 5596
Re: [meloyelo] file creation

Yes, I accidentially left off the last part. Actually, I'm going to be using rsync instead of cp since I'm going from 1 server to another keeping same directory structure but wanted to be able to use this perlscript as a utility to run on any server giving it the 2 arguments to construct the directories.

rsync /opt/oracle/oradata01/redo1.dbf destinationserver:/opt/oracle/oradata01/redo1.dbf

rsync /opt/oracle/oradata01/control1.dbf destinationserver:/opt/oracle/oradata01/control1.dbf

rsync /opt/oracle/oradata02/redo1.dbf destinationserver:/opt/oracle/oradata02/redo1.dbf

rsync /opt/oracle/oradata03/subdir/usertablespace.dbf destinationserver:/opt/oracle/oradata03/subdir/usertablespace.dbf


New User

Jun 17, 2008, 6:48 AM

Views: 5595
Re: [KevinR] file creation


Thanks for the help. That's enough to get me going in the right direction.


Jun 17, 2008, 8:04 AM

Views: 5591
Re: [1951g] file creation

You may not have to write any perl at all -- rsync has the capability to perform recursive copies and also to match file names (see the --include and --exclude options.)