in reply to Re^3: Processing data in on dir and copying, then processing into another dir
in thread Processing data in on dir and copying, then processing into another dir

Hi Zentara, I am using absolute paths. I have set up to testdir for testing this script. When I run the script the stdout is: GLOB(0x804cbdc) . .. RESULTS 2008 RESULTS 2009 RESULTS 2007 The glob should be the files that match from grep, but I nothing is being moved from 'testdir1' to 'testdir2'.
#!/usr/bin/perl use warnings; use strict; # goes from current dir, and looks thru # a subdir named 7, for subdirs starting with # Jan_2009_, then goes into that subdir and finds # files begiining with a or b or c # making your particular regexes is up to you #opendir ( my $rs, "tarfs-orion/orion/Data/Sequencing_Results/RESULTS +2009"); my $topdir = "/home/mgavi/testdir1"; my @subdirs = get_sub_dirs($topdir); my $newdir = "/home/mgavi/testdir2"; sub get_sub_dirs { my $dir = shift; opendir my $dh, $dir or die "Error: $!"; print "$dh\n"; my @dirs = readdir $dh; print "@dirs\n"; @dirs = grep /^Results(.*)$/,@dirs; closedir $dh; return @dirs; } print "@subdirs\n"; my @files; foreach my $dir(@subdirs){ opendir my $dh, "$topdir/$dir" or die "Error: $!"; @files = readdir $dh; print "@files\n"; @files = grep /^(\d+\D\d)_(LacZ|pgK|SD|SU)(.*)$/,@files; foreach my $file(@files){ system( "mv $file $newdir/$file"); } closedir $dh; } print "@files\n";
  • Comment on Re^4: Processing data in on dir and copying, then processing into another dir
  • Download Code

Replies are listed 'Best First'.
Re^5: Processing data in on dir and copying, then processing into another dir
by zentara (Cardinal) on Jan 11, 2009 at 14:30 UTC
    Yeah, you have path issues, and possibly regex issues. mv requires full path names. The following works. In testdir1 I have 2 subdirs Results_2008 and Results_2009. In those subdirs , I have files:
    Results_2008: 2008_12112L5_LacZ 2008_12112L5_pgK 2008_12112L5_SD 2008_12112L5_SU Results_2009: 2009_12112L5_LacZ 2009_12112L5_pgK 2009_12112L5_SD 2009_12112L5_SU
    this script moves them all, and notice the regex(probably can be improved) and full path in the system mv
    #!/usr/bin/perl use warnings; use strict; my $topdir = "/home/zentara/testdir1"; my @subdirs = get_sub_dirs($topdir); my $newdir = "/home/zentara/testdir2"; sub get_sub_dirs { my $dir = shift; opendir my $dh, $dir or die "Error: $!"; # print "$dh\n"; #this is your glob my @dirs = readdir $dh; @dirs = grep /^Results(.*)$/,@dirs; closedir $dh; return @dirs; } print "subdirs = @subdirs\n"; foreach my $dir(@subdirs){ opendir my $dh, "$topdir/$dir" or die "Error: $!"; my @files = readdir $dh; print "raw files = @files\n"; @files = grep /^[\d]+_[\dL]+_(LacZ|pgK|SD|SU)(.*)$/,@files; print "$dir -> @files\n"; foreach my $file(@files){ system( "mv $topdir/$dir/$file $newdir/$file"); } closedir $dh; }
    Also remember, if you want to separate your files out into subdirs, you need to make the subdirs first, before trying to mv them there. See cloning a directory tree

    I'm not really a human, but I play one on earth Remember How Lucky You Are
      My first step is to get the files in Results 2009 that match the regex to mv to a new dir. Next, in the new dir I want to match all the file with dir that have the same id number.
      Ex. 2009_12112L5_LacZ 2009_12112L5_pgK 2009_12112L5_SD 2009_12112L5_SU >
      12112, so each of the files will go into a new dir called 12112/12112_sa.
      I think a hash should help me with the matching. Much Thanks!
      Did I mention that OS is Unix? Also after testing the code it returns:

      GLOB(0x804cbdc)
      . .. RESULTS 2008 RESULTS 2009 RESULTS 2007
      subdirs =
      nothing is mv to newdir