Finding missing files that are named sequentially with Perl?

Hello

I am new to Perl, in fact I am on chapter one of the book. :slight_smile: However I am in need of a Perl Script faster than I can finish the book. Perhaps someone can help me with my immediate need while I read my book.

I have a directory with hundreds of files that are all named like ABCD_21308_0636456392_001.ARC with the 21308 part of this file name being the sequential part of the files. Files come and go from the folder in which they reside with the oldest going first while new ones are created in the folder. The goal is to find missing files in the sequential order as they occure so as to retreive the missing file from the source in a timely fashion.

Files:

ABCD_21308_0636456392_001.ARC
ABCD_21309_0636456392_001.ARC
ABCD_21310_0636456392_001.ARC
ABCD_21311_0636456392_001.ARC

If those are an Oracle archived redo log files just use the rman delete input clause during the backup. If that's the case, there is no need to reinvent the wheel.

backup archivelog  all delete input

No they are not Oracle log files. In fact the file extention used here is fictional. Its actually .txt

Something like this:

perl -le'
    %files = map { ( split "_" )[1] => 1 } glob "*.ARC";
    @seq = sort { $a <=> $b } keys %files;
    print join "\n", grep !$files{$_}, $seq[0] .. $seq[$#seq];
  '

You should adjust the glob for your needs (ARC => txt).

In Perl 5.10 you don't need the hash:

perl -E'
    @seq = sort { $a <=> $b } map { ( split "_" )[1] } glob "*.ARC";
    for ( $seq[0] .. $seq[$#seq] ) {
        say unless $_ ~~ @seq;
    }'

P.S. Now that I think ..., I'm taking min and max values from the available files.
Is that a correct assumption? What if a file below or above that boundaries is missing?

Thank you radoulov ... your code was exactly what I needed. :):b: This will make life easier while I learn Perl.