【问题标题】:Checking a directory and matching with file检查目录并与文件匹配
【发布时间】:2012-06-28 14:45:27
【问题描述】:

我想查看名为 missing 的文件,然后查看名为 flags 的目录。

missing 中列出的每个文件都将始终出现在flags 目录中。

我想查看flags 目录中的每个文件,然后查看它们是否在missing 文件中。如果其中一个不是,请从flags 目录中删除该文件。

    @flags=`ls $dir`;
    $flags_size = scalar @flags;

    $file = "/home1/t01jkxj/check_st/missing";
    $filesize = -s $file;

    if ($filesize < $flags_size) {
      ##What to do??##
    }

【问题讨论】:

  • 为什么要比较$file的大小和@flags数组的元素个数?
  • @TLP:我认为你很清楚 OP 不理解 -s 运算符。
  • 所以您想删除名为flags 的目录中未出现在名为missing 的文件中的所有文件?

标签: perl file directory


【解决方案1】:

您没有描述missing 文件的格式,但我猜它每行包含一个文件并提供文件的完整绝对路径。如果我猜错了,你需要调整这个解决方案。

此程序将missing 文件加载到哈希中。每个哈希元素都以文件名作为键,值为 1。

flags目录打开,每个文件名加上路径,形成$filename中的绝对路径。如果文件名没有出现在 %missing 哈希中,则打印文件名。要真正删除文件,unlink 行应该被取消注释。

use strict;
use warnings;

my $missing = "/home1/t01jkxj/check_st/missing";

open my $fh, '<', $missing or die qq(Unable to open "$missing" for read: $!);
my %missing;
while (<$fh>) {
  next unless /\S/;
  chomp;
  $missing{$_} = 1;
}

my $dir = '/path/to/flags';

opendir my $dh, $dir or die qq(Unable to open directory "$dir": $!);

for my $file (readdir $dh) {
  my $filename = "$dir/$file";
  unless ($missing{$filename}) {
    # unlink $filename;
    print qq(File "$filename" deleted as not found in 'missing' file\n);
  }
}

【讨论】:

    【解决方案2】:

    检查哈希。将所有丢失的条目放入哈希中。然后遍历 flags 目录中的所有文件并检查它是否在哈希中。如果是,很好,如果不是,删除文件。

    my %missings = map { chomp; $_ => 1 } do {
      open my $fh, '<', $missing_file or die "Can't read $missing_file: $!";
      <$fh>
    };
    
    opendir my $dh, $dir or die "Can't read from $dir: $!";
    while(readdir $dh) {
        unlink $_ unless delete $missings{$_};
    }
    
    # I know, you said this can't happen.
    if (keys %missings) {
        print "The following are in $missing_file but not in $dir:\n";
        print "    $_\n" for sort keys %missings;
    }
    

    警告:完全未经测试。我在我的网络浏览器的框中输入了这个。

    【讨论】:

    • 您的警告是有根据的。您的代码将尝试取消链接 ... (但它可能会失败,因为取消链接不能正常删除目录),并且在错误的目录中。
    【解决方案3】:

    在我看来,您也可以使用 bash 命令执行此操作。比如:

    cd /path/to/flags; ls | grep -vf missing.txt | xargs rm
    

    注意:请不要在未测试的情况下运行上述程序。

    在 perl 中,在代码中略显冗长并发出警告可能是个好主意。当然,可以删除自动化作业的警告。

    use strict;
    use warnings;
    
    my $dir = "/path/to/flags";
    chdir $dir or die $!;        # change working directory
    my @flags = <*>;             # get a list of the files
    my $file = "/home1/t01jkxj/check_st/missing";
    open my $fh, "<", $file or die $!;
    chomp(my @missing = <$fh>);  # get file names and remove newlines
    my %missing = map { $_ => 1 } @missing;   # ..and put them in a hash
    
    my @delete;
    for my $file (@flags) {      # all files not in the hash go into @delete
        push @delete, $file unless $missing{$file};
    }
    
    if (@delete) {   # do not delete without confirmation
        print @delete . " files to delete\n@delete\n---\nDelete them all? ";
        my $reply = <>;
        if ($reply =~ /^y$/) {
            unlink $_ or warn "$_: $!" for @delete;
        }
    } else {
        print "No missing files to delete.\n";
    }
    

    【讨论】:

      【解决方案4】:

      目前不在 Linux 中,但这是您需要做的。该脚本收集文件和数组目录中的文件列表,然后找出两者的不同之处。我会测试但不能真的)-=。将其视为伪代码!:

      use strict;
      use warnings;
      my $fi;
      my $line;
      my @to_delete;
      my $var;
      my @indir;
      my @files;
      # the difference of @females and @simpsons
      @indir = `ls`;
      
      open($fi, "< list.txt");
      while ($line = <$fi>) 
      {
          chomp($line);
          push @files, $line;
      }
      @to_delete=grep(!defined $indir{$_}, @files); #gets difference of the two arrays
      
      
      print "Delete this:\t$_\n" foreach (@to_delete);
      

      【讨论】:

      • 你不能像这样使用 chomp:push @files, chomp($line)chomp 返回从其参数中删除的字符总数,而不是参数本身。此外,您不能像这样 grep,因为您在 %flags 哈希中没有任何键。
      • 谢谢 - 这些都是我的巨大错误。让我知道其他人。或者只是编辑使其正确,因为我可能会采取太多草稿嘿。
      猜你喜欢
      • 2019-02-06
      • 2013-01-07
      • 2016-07-04
      • 2021-03-28
      • 2022-12-11
      • 2018-03-12
      • 2023-01-19
      • 1970-01-01
      • 2014-02-04
      相关资源
      最近更新 更多