#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2000
    Posts
    73
    Rep Power
    15
    how do i copy a directory and its contents from a perl/cgi script?
  2. #2
  3. No Profile Picture
    freebsd
    Guest
    Devshed Newbie (0 - 499 posts)
    #!/usr/local/bin/perl

    $from = "/full/path/to/foo/bar";

    $to = "/full/path/to/another_dir/bar";

    print "Content-type: text/htmlnn";
    `cp -R $from $to`;
    print "Done";

    Please note, if Apache is run as nobody:nogroup, you need to chmod "another_dir" to 757 in advance. If it's run as, let say "www" and the "bar" is also belong to "www" group, then chmod 775 should be fine. If neither ones works, chmod it to 777.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2000
    Location
    Indiana
    Posts
    614
    Rep Power
    16
    Better make this:
    `cp -R $from $to`;
    this:
    `cp -R -f $from $to`;

    This will over right the file if it already exists (for backing up on a regular basis).
  6. #4
  7. No Profile Picture
    RyanP
    Guest
    Devshed Newbie (0 - 499 posts)
    wouldn't you want

    `cp -Rf $from $to`;

    instead? I've heard that not combining options can cause problems
  8. #5
  9. No Profile Picture
    freebsd
    Guest
    Devshed Newbie (0 - 499 posts)
    >>This will over right the file if it already exists

    In my example, /full/path/to/another_dir/bar, the "bar" directory doesn't exist. There is no point to use the -f flag.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Aug 2000
    Location
    Indiana
    Posts
    614
    Rep Power
    16
    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>wouldn't you want
    `cp -Rf $from $to`;

    instead? I've heard that not combining options can cause problems
    [/quote]
    Yeah, could be. I just know that it doesn't mess up on my OS, so that's what I use.

    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>In my example, /full/path/to/another_dir/bar, the "bar" directory doesn't exist. There is no point to use the -f flag.[/quote]
    Not the 1st time the script is run. But if it's setup to run on say a crontab, then it'll need to over right the old files each time.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Posts
    81
    Rep Power
    15
    A more portable approach would be to use the File::Copy and File::Find modules (with File::Path to make directories). The following is untested, but should work:

    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
    use File::Copy;
    use File::Find;
    use File::Path;

    my $source = 'mysourcedir';
    my $target = 'mytargetdir';

    find( copy_files, $source );

    # note: for the copy sub, $_ is set to the current file being tested, and we are chdir'd to the directory that file resides in.
    sub copy_files {
    my $targetdir = $File::Find::dir;
    $targetdir =~ s/Q$source/Q$target/o;
    mkpath( $targetdir ) if not -e $targetdir;
    copy($_, "$target/$_") if -f;
    }
    [/code]

    It would be a trivial task (and probably one that has already been done, though I haven't checked CPAN) to write a portable module that performs a recursive copy using this code. Currently, this performs a recursive copy. This behaviour could be changed by setting $File::Find:: prune = 1, thereby preventing the copy descending into subdirectories (apologies, had to put a space between Find:: and prune which shouldn't be there as the damn messaging system was ignoring the 'disable smilies' option when editing...)

    [This message has been edited by christucker2 (edited August 16, 2000).]
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2000
    Posts
    73
    Rep Power
    15
    ` ` did the trick


    Thank yo'all !

IMN logo majestic logo threadwatch logo seochat tools logo