Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Preserving Ownership with File::Copy::Recursive

by xorl (Deacon)
on Oct 11, 2012 at 12:03 UTC ( [id://998420]=perlquestion: print w/replies, xml ) Need Help??

xorl has asked for the wisdom of the Perl Monks concerning the following question:

I want to copy a directory (including subdirs) and preserve the ownership. This is a brief example that isn't working.

The directory:

ls -l ./foo drwxrwxr-x 2 xorl xorl 4096 2011-10-10 09:38 ./ drwxrwxr-x 10 xorl xorl 20480 2011-10-10 07:47 ../ drwxrwxr-x 2 tcat xorl 4096 2011-10-10 07:52 bar/ -rw-rw-r-- 1 tcat xorl 0 2011-10-10 09:21 bar.txt -rw-rw-r-- 1 xorl xorl 0 2011-10-10 09:38 foobar.txt
(while there is stuff in the bar directory, it doesn't seem to matter for this example)

#!/usr/bin/perl use strict; use warnings; use File::Copy::Recursive qw(dircopy); my $src = "./foo"; my $dest = "./foo2"; dircopy($src, $dest);

And I get:

ls -l ./foo2 drwxrwxr-x 2 xorl xorl 4096 2011-10-11 07:55 ./ drwxrwxr-x 10 xorl xorl 20480 2011-10-11 07:55 ../ drwxrwxr-x 2 xorl xorl 4096 2011-10-11 07:55 bar/ -rw-rw-r-- 1 xorl xorl 0 2011-10-11 07:55 bar.txt -rw-rw-r-- 1 xorl xorl 0 2011-10-11 07:55 foobar.txt

Looking at the documentation for File::Copy::Recursive, I don't see where it says anything about being able to preserve the ownership. My next thought is to try something combination of File::Find and File::Copy but that seems overly complex for something I think should be simple. Plus I don't see in the documentation where that would preserve ownership either.

So how should I go about recursively copying a directory while preserving ownership?

As noted in one of the replies, the script will be run by root.

Edit: Fixed typo and added more info

Edit #2: I'm giving up on a perl solution for now (but would still like to see one if it is possible). Going to write a bash script that uses either cp or maybe rsync instead.

Replies are listed 'Best First'.
Re: Preserving Ownership with File::Copy::Recursive
by BrowserUk (Patriarch) on Oct 11, 2012 at 13:15 UTC

    I have vague recollections of a similar problem a few years ago under hp/ux and the solution that was settled upon was to tar the subdir to be copied and untar it in the destination. This has the required side effect of preserving the attributes.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    RIP Neil Armstrong

    .
Re: Preserving Ownership with File::Copy::Recursive
by zentara (Archbishop) on Oct 11, 2012 at 12:24 UTC
    And I get: ls -l ./foo

    Shouldn't that second ls be for ./foo2? I do see the tcat change. It seems normal behavior to me, that ownership goes to whoever is doing the copy. For instance, if you copy some root files to your homedir, ownership will given to the owner of the homedir, unless the copy is done by root. You would be violating the principles of su and sudo otherwise, i.e. escalation of priviledges could occur. But I may be wrong. ;-)


    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh

      Yes that was a typo. It should have been ls -l ./foo2.

      The script will be run by root.

Re: Preserving Ownership with File::Copy::Recursive
by Anonymous Monk on Oct 12, 2012 at 02:07 UTC

    Hi,

    As you are running as root, could you use chown and chgrp after File::Copy?

    J.C.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://998420]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (None)
    As of 2024-04-25 01:05 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found