File Copy / Delete 
Author Message
 File Copy / Delete

Hello all!

I know how to copy files and directories as well as delete them - one at
a time. Does anyone here know how to do this in Perl 'en mass'? In other
words, I would like to copy a folder and all its contents with a single
statement instead of having to write a separate copy statement for each
item in that folder. Likewise I would like to be able to delete a folder
and all its sub contents, including sub folders, with a single
statement. Suggestions?

Albert Dewey



Sat, 25 Jan 2003 03:00:00 GMT  
 File Copy / Delete

Quote:

> I would like to copy a folder and all its contents with a single
> statement ...
> Likewise I would like to be able to delete a folder
> and all its sub contents, including sub folders, with a single
> statement.

Have you checked out the File::Copy module in CPAN?

--
John Porter



Sat, 25 Jan 2003 03:00:00 GMT  
 File Copy / Delete
Nope but I will. I have been using copy.pm for basic copy commands. I
did get an answer from a guy in Denmark that I am going to try. He

system ("cp -p $source $target");

and

system ("rm -fr $directory");

Which looks very much what I need.

Thanks,

Al

Quote:


> > I would like to copy a folder and all its contents with a single
> > statement ...
> > Likewise I would like to be able to delete a folder
> > and all its sub contents, including sub folders, with a single
> > statement.

> Have you checked out the File::Copy module in CPAN?

> --
> John Porter



Sat, 25 Jan 2003 03:00:00 GMT  
 File Copy / Delete

Quote:

> system ("cp -p $source $target");

> and

> system ("rm -fr $directory");

> Which looks very much what I need.

Except that it 'can' be fairly dangerous.

$source = '/home/someuser/passwd.hack';
$target = '/etc/passwd';

system ("cp -p $source $target");

$directory = '/';

system ("rm -fr $directory");



Sun, 26 Jan 2003 03:00:00 GMT  
 File Copy / Delete

Quote:

> > system ("cp -p $source $target");
> > system ("rm -fr $directory");
> > Which looks very much what I need.

> Except that it 'can' be fairly dangerous.

> $source = '/home/someuser/passwd.hack';
> $target = '/etc/passwd';

> system ("cp -p $source $target");

> $directory = '/';

> system ("rm -fr $directory");

No, system() doesn't magically give you permission to do things
you couldn't otherwise do.

But there are good reasons for not using system() when the
desired functionality is available in perl.

It is slower.  The execution of an external program involves
quite a bit of overhead, compared to executing a few perl ops.
Of course, for many iterations, the cost of starting the external
program may be mitigated -- for example, when you have to grep
through a file of 10 million lines.   The startup costs are
compounded if you use the single-arg form of system() which
also spawns a shell process.

Also, if you implement the whole algorithm in perl, you get finer
grained control over the process.  With external programs, they
either finish or they die; if you're lucky, you'll get some kind
of status code back to help you figure out what went wrong.

Then there's the whole tainting thing.

--
John Porter



Sun, 26 Jan 2003 03:00:00 GMT  
 File Copy / Delete

Quote:

> > Except that it 'can' be fairly dangerous.
[snipe]
> > $directory = '/';

> > system ("rm -fr $directory");

> No, system() doesn't magically give you permission to do things
> you couldn't otherwise do.

But "rm -fr /" is still going to cause a lot of "GRINK GRONK"ing
and do a lot of damage to your account. I think I would call that
dangerous, wouldn't you? Now "deltree /y c:\windows", that's one
of my favourite commands and hardly dangerous at all.

cheers
Beable van Polasm
--
WHAT WOULD WILLIAM SHATNER DO?                         IQC 78189333
I was really surprised to be asked here tonight to honour Bob Hope.
Surprised isn't the right word... annoyed -- Ronald Reagan
http://members.xoom.com/_______/news/index.html



Mon, 27 Jan 2003 03:00:00 GMT  
 File Copy / Delete

Quote:

> No, system() doesn't magically give you permission to do things
> you couldn't otherwise do.

As I said.. It 'Could' be dagerous. But then again
noone is stupid enough to login as root all the time :)

later,
        Tom fjellstrom



Tue, 28 Jan 2003 03:00:00 GMT  
 File Copy / Delete

Quote:

> > No, system() doesn't magically give you permission to do things
> > you couldn't otherwise do.

> But "rm -fr /" is still going to cause a lot of "GRINK GRONK"ing
> and do a lot of damage to your account. I think I would call that
> dangerous, wouldn't you?

Sure; but it doesn't differentiate system() from any other typical
means of recursively deleting directories.

Of course, if you hand-roll it, you can make it so that non-writable
and/or non-executable directories aren't recursed at all...

Quote:
> Now "deltree /y c:\windows", that's one
> of my favourite commands and hardly dangerous at all.

:-)

--
John Porter

        Aus tiefem Traum bin ich erwacht.



Tue, 28 Jan 2003 03:00:00 GMT  
 File Copy / Delete
In comp.lang.perl.moderated, on Tue, 08 Aug 2000 08:28:10 -0400
Quote:

> Hello all!
> I know how to copy files and directories as well as delete them - one at
> a time. Does anyone here know how to do this in Perl 'en mass'?

[snippage]

Yes.  I'm not sure why you *want* to, however, unless you're writing
part of the "implement all known unix utilities in bare Perl" scriptset.

What's wrong with cp -a?  Or the good old:

% (cd foo && tar cf -)| (cd bar && tar xf -)

trick?

Quote:
> Suggestions?

Do it recursively, if you must.  There's gotchas to do with symlinks,
and .. in both cases, and something niggles at the memory telling me
that there's more gotchas... probably to do with preservation of file
permissions and timestamps and a bunch of other things.

I really recommend *not* writing this code in Perl, and simply calling
an existing external utility.

Later,

   Thorf

--

 Internet and Open Source Development, Consulting and Training.
 Netizen Pty Ltd, GPO Box 2265U, Melbourne VIC 3000, Australia.
Tel: +61 3 9614 0949  Mob: +61 411 692 516  Fax: +61 3 9614 0948



Tue, 28 Jan 2003 03:00:00 GMT  
 File Copy / Delete
For everyone who has responded to my initial question for help -

I see that my question has yielded a number of interesting responses and
warnings including the question as to why I would want to do this. I will
try to help clear the air here by explaining my purpose. First of all, this
will not be a publicly available function, rather it will be in a password
protected part of the site where only I will have access. The purpose will
be to create a new account for users who subscribe to a service that I have
created. Part of this service includes a fairly complex number of prewritten
html, data files, configuration info, and perl routines to which the new
account subscriber will have access, but only from within the directory that
I create for them. I have a source directory from which I am copying the
entire system from and a directory that I set up for the owner of the new
account where this copy is to be placed. Currently, I have written a routine
that first creates the directories necessary within the new user's account
directory, then I copy, one at a time, all the files necessary from the
source directory into their proper locations in the new account location.
Then I have code that sets permissions, htaccess stuff, and chmod info as
needed. Looking at my code it is a really long arrangement of a whole lot of
copy commands and I was just looking for a sompler way to take the entire
source directory and make a copy of its contents, sub folders and all, into
a new directory that I set up for the new account. The delete part of my
question was to perform the opposite function so I could remove an account
just as easily. Again, I reiterate that this code will only be accessible by
me and not the general public. I say this hoping to help clear any confusion
as to my intentions for this function.

Thanks,

Albert Dewey

Quote:

> Hello all!

> I know how to copy files and directories as well as delete them - one at
> a time. Does anyone here know how to do this in Perl 'en mass'? In other
> words, I would like to copy a folder and all its contents with a single
> statement instead of having to write a separate copy statement for each
> item in that folder. Likewise I would like to be able to delete a folder
> and all its sub contents, including sub folders, with a single
> statement. Suggestions?

> Albert Dewey



Tue, 28 Jan 2003 03:00:00 GMT  
 File Copy / Delete
I want to thank all the posters here for the wide variety of responses to my
query. What I would up using was a piece of code very similar to one I
received in my email directly. Here are the two snippets that I am using and
that work perfectly for my purposes:

To recursively copy the contents of a directory -
  $Source = "../source";
  $Target = "../$Folder";
  system ("cp -r $Source $Target");

And to remove a directory and all of its contents -
 $Directory = "../$Folder";
 system ("rm -fr $Directory");

This code allowed me to trim almost 20K of file size from my cgi script that
uses it. Now you probably can appreciate just why I was so eager to have
something trim and lean. (an added bonus is that my script now loads up in
notepad once again!) In so far as cpu overhead is concerned, this is not a
worry even though there are probably more efficient processes than calling a
system command for this but since it manages to execute in the blink of an
eye and in practicality won't be used for more than just a few times in any
given day, this really is not an issue. With regards to security, the script
that uses these codes is in a password protected administration folder and
is for my personal use only and will not be available to the public.

Thanks to everyone for their replies because I did learn a lot from your
different methods even if I did not use them.

Albert Dewey

Quote:

> Hello all!

> I know how to copy files and directories as well as delete them - one at
> a time. Does anyone here know how to do this in Perl 'en mass'? In other
> words, I would like to copy a folder and all its contents with a single
> statement instead of having to write a separate copy statement for each
> item in that folder. Likewise I would like to be able to delete a folder
> and all its sub contents, including sub folders, with a single
> statement. Suggestions?

> Albert Dewey



Wed, 29 Jan 2003 03:00:00 GMT  
 File Copy / Delete


Quote:
>will not be a publicly available function, rather it will be in a password
>protected part of the site where only I will have access. The purpose will
>be to create a new account for users who subscribe to a service ...

I recommend that you split account creation into two processes.
1) A CGI process that parses the HTML form and puts a "create account" request
   into a queue.
2) A cron job that periodically scans the queue, re-validates all the
   fields in the request, then performs account creation.  This process
   runs under you userID (instead of "nobody" like CGI processes do).

One method I've used to duplicate a directory tree is:
        chdir $source_directory or die;
        mkdir $destination_directory,0755 or die;
        system "tar cf - . | (cd $destination_directory; tar xvf -)" or die;

If you have a system that has the 'tar' program, there are occasions where
it is more appropriate to use it instead of roll-your-own using File::Copy.
        -Joe
--
See http://www.inwap.com/ for PDP-10 and "ReBoot" pages.



Thu, 30 Jan 2003 03:00:00 GMT  
 
 [ 12 post ] 

 Relevant Pages 

1. Weird char>string conversion

2. Deleting and copying files in Win95

3. Using File::Copy and Sockets to copy public files from http addresses

4. File::Copy, copying files problem.

5. Binary -font file for Pascal

6. DBGrid: How do I make a sum, of all the numbers occuring in a column?

7. Warning using copy from File::Copy

8. Semantics of File::Copy::copy(,)

9. unwanted UNIX file permissions after using File::Copy::syscopy

10. File::Copy WIn32 bad file descriptor

11. How do copy files based on pattern on original file namnes

12. Fixed-Point variables

 

 
Powered by phpBB® Forum Software