954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to move and consolidate files from within a large selction of folders, with perl?

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

BioJavaPhobic
Newbie Poster
11 posts since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

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

roswell1329
Junior Poster in Training
71 posts since May 2006
Reputation Points: 21
Solved Threads: 2
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: