shell脚本过滤du并通过子文件夹中文件内的字符串查找

发布于 2024-09-03 11:11:17 字数 1412 浏览 6 评论 0原文

我在 cygwin 上运行以下命令:

查找 /cygdrive/d/tmp/* -max深度 0 -mtime -150 -类型 d | xargs du --max-深度 = 0 >文件夹大小报告.csv

我打算使用此命令执行以下操作:

对于在过去 150 天内修改的 /d/tmp/ 下的每个文件夹,检查其总大小(包括其中的文件)并将其报告给文件文件夹大小报告.csv 然而现在这对我来说还不够好,因为事实证明每个

<代码>/d/tmp/subfolder1/somefile.properties
/d/tmp/subfolder2/somefile.properties
/d/tmp/subfolder3/somefile.properties
/d/tmp/subfolder4/somefile.properties

正如您在每个子文件夹X中看到的那样,有一个名为 somefile.properties 的文件 它里面有一个属性 SOMEPROPKEY=3808612800100 (以及其他属性)

这是以毫秒为单位的时间,我需要更改命令,以便代替 -mtime -150 它仅包含在整个计算中 subfolderX 内有一个文件 somefile.properties,其中 SOMEPROPKEY=3808612800100 是未来的时间(以毫秒为单位),如果值 SOMEPROPKEY=23948948已过去,则根本不包含该文件夹 在 foldersizesreport.csv 中,因为它与我无关。

所以结果报告应该是这样的:

/d/tmp/,subfolder1,<其大小(以 KB 为单位)>
/d/tmp/,子文件夹2,<其大小(以 KB 为单位)>

如果 subfolder3 有一个 SOMEPROPKEY=34243234 (过去的时间以毫秒为单位),那么它就不会出现在该 csv 文件中。

所以基本上我正在寻找:

查找 /cygdrive/d/tmp/* -max深度 0 -mtime -150 -type d | 
   <仅包含文件属性的子文件夹 
   SOMEPROPKEY=28374874827 - 未来的时间(以毫秒为单位) 
   不是过去| xargs du --max-深度 = 0 >文件夹大小报告.csv

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悲歌长辞 2024-09-10 11:11:18

这是整个事情的 Perl 版本:

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;

使用

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

输出(带有您的示例)

/home/regis/stackoverflow/2937940/,subfolder1,16K
/home/regis/stackoverflow/2937940/,subfolder2,16K
/home/regis/stackoverflow/2937940/,subfolder4,16K

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文