views:

186

answers:

1

I have the following command that I run on cygwin:

find /cygdrive/d/tmp/* -maxdepth 0 -mtime -150 -type d | xargs du --max-depth=0 > foldersizesreport.csv

I intended to do the following with this command:

for each folder under /d/tmp/ that was modified in last 150 days, check its total size including files within it and report it to file foldersizesreport.csv however that is now not good enough for me, as it turns out inside each

/d/tmp/subfolder1/somefile.properties
/d/tmp/subfolder2/somefile.properties
/d/tmp/subfolder3/somefile.properties
/d/tmp/subfolder4/somefile.properties

so as you see inside each subfolderX there is a file named somefile.properties inside it there is a property SOMEPROPKEY=3808612800100 (among other properties)

this is the time in millisecond, i need to change the command so that instead of -mtime -150 it will include in the whole calculation only subfolderX that has a file inside them somefile.properties where the SOMEPROPKEY=3808612800100 is the time in millisecond in future, if the value SOMEPROPKEY=23948948 is in past then dont at all include the folder in the foldersizesreport.csv because its not relevant to me.

so the result report should be looking like:

/d/tmp/,subfolder1,<itssizein KB>
/d/tmp/,subfolder2,<itssizein KB>

and if subfolder3 had a SOMEPROPKEY=34243234 (time in ms in past) then it would not be in that csv file.

so basically I'm looking for:

find /cygdrive/d/tmp/* -maxdepth 0 -mtime -150 -type d | 
   <only subfolders that have in them property in file 
   SOMEPROPKEY=28374874827 - time in ms in future and 
   not in past | xargs du --max-depth=0 > foldersizesreport.csv
+2  A: 

Here's a perl version for the whole thing:

filter.pl

#!/usr/bin/perl

use strict;
use warnings;
use File::Spec;

# -------------------------- configuration ----------------------------
my %CFG = (
       'propertiesFile' => 'somfile.properties',
       'propertyKey' => 'SOMEPROPKEY',
       'duCommand' => 'du -Bk -s'
);
# ---------------------------------------------------------------------

while (my $dir = <>) {
       chomp $dir;
       open(my $F, File::Spec->catfile($dir, $CFG{"propertiesFile"})) || next;
       my ($match) = grep /$CFG{"propertyKey"}=\d+/, <$F>;
       close $F;

       if ($match =~ m/$CFG{"propertyKey"}=(\d+)/) {
               my ($volume, $directories, $file) = File::Spec->splitpath($dir);
               my $command = "$CFG{'duCommand'} $dir";
               # on Windows you might need $volume this assumes Unix-like filesystem
               print $directories . "," . $file . "," .
                       `$command | cut -f1` if $1 > time();
       }
}

exit;

usage

find /home/regis/stackoverflow/2937940/* -maxdepth 0 
  -mtime -150 -type d | ./filter.pl

output (with your sample)

/home/regis/stackoverflow/2937940/,subfolder1,16K
/home/regis/stackoverflow/2937940/,subfolder2,16K
/home/regis/stackoverflow/2937940/,subfolder4,16K
RC
Jason
You can use inline code in comment by surrounding text with backticks giving you something like `that` ;)
RC
HiI have a few questions:what is the meaning of this: `my $dir = <>`where does it take the $dir parameter from? where does it take the input of the directory? i didn't see any reference to some input...what is the meaning of `|| next`i can't figure out how come its processing each directory with like a loop is the `|| next` a loop? if yes a loop of what?Thanks
Jason
`<>` means stdin; the `|| next` is here to handles `open` failures: when open fails, we skip the directory.
RC