ramjamman:

If you're on a *NIX box and have a text file containing a list of files to copy to a new location, you don't even need to use perl to do the copy. Instead, you can use xargs and tar to do the job. As an example, here's a text file with a list of files to copy:

$ cat mylist.txt foo bar bim/bam

And in our source directory, we have the specified files, along with a file we want to ignore:

$ ls -alR source source: total 3.0K drwxr-xr-x+ 1 Roboticus None 0 Jul 21 00:51 ./ drwxr-xr-x+ 1 Roboticus None 0 Jul 21 00:49 ../ -rw-r--r-- 1 Roboticus None 4 Jul 21 00:47 bar drwxr-xr-x+ 1 Roboticus None 0 Jul 21 00:48 bim/ -rw-r--r-- 1 Roboticus None 8 Jul 21 00:47 foo -rw-r--r-- 1 Roboticus None 7 Jul 21 00:51 ignore source/bim: total 1.0K drwxr-xr-x+ 1 Roboticus None 0 Jul 21 00:48 ./ drwxr-xr-x+ 1 Roboticus None 0 Jul 21 00:51 ../ -rw-r--r-- 1 Roboticus None 13 Jul 21 00:48 bam

At this time, our dest directory doesn't have anything interesting in it:

$ ls -alR dest dest: total 0 drwxr-xr-x+ 1 Roboticus None 0 Jul 21 00:51 ./ drwxr-xr-x+ 1 Roboticus None 0 Jul 21 00:49 ../

So all we need to do is to start a process, change to the source directory and then use xargs to tell tar which files to copy into an archive. Then we'll pipe that archive to another process wherein we change to the destination directory and extract all the files, like this:

$ (cd source; xargs <../mylist.txt tar cf -) | (cd dest; tar xf -)

After running that, the files we wanted to copy to dest are there:

$ ls -alR dest dest: total 2.0K drwxr-xr-x+ 1 Roboticus None 0 Jul 21 00:52 ./ drwxr-xr-x+ 1 Roboticus None 0 Jul 21 00:49 ../ -rw-r--r-- 1 Roboticus None 4 Jul 21 00:47 bar drwxr-xr-x+ 1 Roboticus None 0 Jul 21 00:52 bim/ -rw-r--r-- 1 Roboticus None 8 Jul 21 00:47 foo dest/bim: total 1.0K drwxr-xr-x+ 1 Roboticus None 0 Jul 21 00:52 ./ drwxr-xr-x+ 1 Roboticus None 0 Jul 21 00:52 ../ -rw-r--r-- 1 Roboticus None 13 Jul 21 00:48 bam

...roboticus

When your only tool is a hammer, all problems look like your thumb.


In reply to Re: Copying a list of files from a directory to a new directory by roboticus
in thread Copying a list of files from a directory to a new directory by ramjamman

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.