Hi,

I have a folder containing a further 2500 folders or so. These all contain files. I want to move the files from within only a selection of around 260 of these folders into a new folder. I have a text list of the folders of interest.

So basically I want to open the folders named in my list, move only the files from these folders and consolidate together into one new folder.

I'm looking at grep etc but I don't know how to implement this on folder names within a directory. Any pointers on where I could start on this would be greatly appreciated.

Thank you

Here's an example that will take all the files from the directories specified and move them to a new directory:

#!/usr/bin/perl -w
use File::Copy;
@dirs_of_interest = ('/home/user/data1',
                     '/home/user/data2',
                     '/home/user/data3');
$new_location = '/home/user/data4';

foreach $dir (@dirs_of_interest) {
    opendir DIRH, $dir or die "Couldn't open $dir: $!\n";
    foreach $file (sort readdir DIRH) {
        move("$dir/$file","$new_location/$file");
    }
    closedir DIRH;
}

If you need to "walk" the file tree to find the directories on your list, that can be done with some more code. Also, if you need to move only select files, look into the Perl function "glob" for identifying filenames using shell shorthand (i.e., '*.txt' or 'ag*.pl').

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.