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.


In reply to Preserving Ownership with File::Copy::Recursive by xorl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.