0

我想查看一个名为的文件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??##
    }
4

4 回答 4

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);
  }
}
于 2012-06-28T18:02:35.380 回答
0

检查哈希。将所有丢失的条目放入哈希中。然后遍历 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;
}

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

于 2012-06-28T15:24:06.397 回答
0

现在不在 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);
于 2012-06-28T15:41:13.617 回答
0

在我看来,您也可以使用 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";
}
于 2012-06-28T16:47:18.467 回答