Just for fun, here's a similar one-liner using File::Find and PDF::Class:

# Formatted for clarity;
use File::Find;
use PDF::Class; .
say for find( dir => ".", name => *.ends-with(".pdf") ).map(
  sub ($_) {
    CATCH { default { return 0 } };
    say "Checking { .absolute }...";
    PDF::Class.open($_).page-count
  }
).sum'

It's a bit larger than the more elegant solution provided by Sean, but it
does include some nice to haves.



On Sat, May 24, 2025 at 5:31 AM William Michels via perl6-users <
perl6-us...@perl.org> wrote:

> Thanks, Sean!
>
> Do any of the various `PDF` modules work to solve your page-counting quest?
>
> I'll have to play around with your code a bit. Normally I would use
> `.dir(test => /:i \.pdf / )`  to pull out PDF files.
> The closest U&L answer posted for your issue might be:
>
>
> https://unix.stackexchange.com/questions/651035/how-to-count-total-number-of-lines-of-all-txt-files/745652#745652
>
> Cheers!
>
>
>
> On May 21, 2025, at 13:08, Sean McAfee <eef...@gmail.com> wrote:
>
> On Tue, May 20, 2025 at 12:51 PM William Michels via perl6-users <
> perl6-us...@perl.org> wrote:
>
>> If you haven't visited U&L StackExchange, you should! It's much less
>> 'siloed' than StackOverflow in that an OP might post a question requesting
>> a bash/sed/awk answer, but other answers are readily accepted (and
>> upvoted!) as well. Some good answers written in:  bash, zsh, sed, awk, jq,
>> mlr, Ruby, Perl, and Raku. Check it out!
>>
>
> Cool!  I'll have to drop by sometime.  I've been moving to use Raku in
> place of shell scripts or commands more often.  A short time ago I wanted
> to get a list of directories along with the total page count of all PDF
> files within them, sorted by the page counts, and I was able to do it with
> a short-ish Raku one-liner (broken into multiple lines here for clarity):
>
> .say for sort dir.map: {
>   run('exiftool', .dir.grep(/'.pdf'$/), :out)
>     .out.slurp.match(:g, /^^ 'Page Count' \N+? <( \d+ $$/).sum => ~$_ if
> .d
> }
>
> The one thing that's hardest for me to keep in mind is to use "\N" instead
> of ".", which here silently produces dramatically wrong results.
>
>
>

Reply via email to