Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

copying a directory and it's contents

by Anonymous Monk
on Apr 04, 2005 at 21:02 UTC ( [id://444800]=perlquestion: print w/replies, xml ) Need Help??

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

Hello monks,

Does anyone know a command to copy a whole directory over to a new dirctory. The directory contains other directories which contain files. So how can I copy all the contents of the main directory over to another directory.

Thank you in advance.

Replies are listed 'Best First'.
Re: copying a directory and it's contents
by lachoy (Parson) on Apr 04, 2005 at 21:08 UTC
Re: copying a directory and it's contents
by Tanktalus (Canon) on Apr 04, 2005 at 21:27 UTC

    I generally do this:

    $ (cd $srcdir; tar cf - *) | (cd $dstdir; tar xf -)
    But that's not entirely perlish. For large amounts of copying (large files, large numbers of files), it may be more efficient to do this simply because then you don't need to do much, and the overhead of forking and setting up pipes is greatly minimised by the actual work. For small numbers of files in small numbers of directories, it's probably still worth it simply to avoid development effort.

    (cf File::DirSync which looks really cool, but if you're not syncing but actually just copying from source to destination, and am not going to need to resync it later, may be a bit of an overkill.)

Re: copying a directory and it's contents
by sh1tn (Priest) on Apr 05, 2005 at 10:37 UTC
    You want to copy directory recursively.
    See File::NCopy.
    ... copy \1,"directory1","directory2" ...


Re: copying a directory and it's contents
by ww (Archbishop) on Apr 04, 2005 at 21:33 UTC
    To me (YMMV) question is ambiguous (to be charitable; UNclear to be precise).

    But for starters, why would you use perl rather than Command Line utilities such as cp or mv (*n*x) or move (DOS) or (if on windoze) drag'n'drop using file manager?

    and, to my eyes, the second question doesn't make a lot of sense, unless you either mean -- move all the files in a root (/ or c:\) and its directories to "another directory" for backup or somesuch while the factual statement between the two questions seems to suggest you may be talking about a non-root directory.

    apologies if I'm being dense, but consider that good precise questions are likely to get the best answers. Please update (Tank points out that since you're an annoymous monk, you'll have to reply or "comment") to clarify!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-04-26 04:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found