0

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

2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by roswell1329
1

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').

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.