【问题标题】:shell script filter du and find by a string inside a file in a subfoldershell脚本过滤du并通过子文件夹中文件内的字符串查找
【发布时间】:2010-05-30 08:13:49
【问题描述】:

我在 cygwin 上运行了以下命令:

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

我打算用这个命令做以下事情:

对于/d/tmp/ 下在过去 150 天内修改的每个文件夹,检查其总大小(包括其中的文件)并将其报告给文件 foldersizesreport.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,<itssizein KB>
/d/tmp/,subfolder2,<itssizein KB>

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

所以基本上我正在寻找:

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

【问题讨论】:

  • 请在您的问题中使用正确的格式,这样可以确保可读性并可能有助于获得答案。
  • 好的,谢谢我尝试更新它我是新来的......

标签: linux shell cygwin awk grep


【解决方案1】:

这是一个 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

【讨论】:

  • 谢谢!!我只需要为已定义...添加一个测试,否则我得到一个错误: if (defined($match) && $match =~ m/$CFG{"propertyKey"}=(\d+)/) { how do i顺便把这个格式化成代码?
  • 您可以在注释中使用内联代码,方法是用反引号包围文本,为您提供类似that ;)
  • 您好,我有几个问题:这是什么意思:my $dir = &lt;&gt; 从哪里获取 $dir 参数?它在哪里输入目录?我没有看到对某些输入的任何引用...|| next 的含义是什么我无法弄清楚它处理每个目录的方式就像一个循环是|| next 一个循环?如果是的话,什么循环?谢谢
  • &lt;&gt; 表示标准输入; || next 在这里处理 open 失败:当打开失败时,我们跳过目录。
猜你喜欢
  • 1970-01-01
  • 2015-09-08
  • 2016-05-14
  • 1970-01-01
  • 1970-01-01
  • 2011-06-28
  • 1970-01-01
  • 2014-01-06
  • 1970-01-01
相关资源
最近更新 更多