[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Is perl still the No.1 language for sysadmin?



Le 3 avril 2023 Emanuel Berg a écrit :

> Michel Verdier wrote:
>
>>> I'm still so impressed by this, I tried to run this but it
>>> seems I lack the Slurp module?
>>
>> apt-get install libfile-slurp-perl
>
> Merci :)
>
> Indeed, works!
>
> Okay, forget about the function/script then, I have it and it
> works :)

Here it is with some more tests.
Easier to read but much less fun :)

#!/usr/bin/perl -w

use strict;

# echo $PATH | tr ':' '\n' | perl -MFile::Slurp -ne 'chomp;@e=read_dir($_,prefix=>1); print map "$_\n",@e'|xargs file|perl -pe 's/\S+\s+//'|grep -v 'symbolic link'|perl -pe 's/, dynamically linked.+//'|sort|uniq -c|sort -rn

my @folders = split(':',$ENV{PATH});
my %count;

foreach my $folder (@folders) {
    chomp($folder);
    next if(-l $folder);
    print "$folder\n";
    opendir(DIR,"$folder");
    my @files = readdir(DIR);
    closedir DIR;
    foreach my $file (@files) {
        next if($file =~ /^\.\.?$/ or -l "$folder/$file");
        chomp(my $type = `file -b $folder/$file`);
        next if($type =~ /symbolic link/);
        $type =~ s/,.+$//;
        $count{$type}++;
    }
}

foreach my $key (sort {$count{$b} <=> $count{$a}} keys %count) {
    printf("%5d %s\n", $count{$key}, $key);
}


Reply to: