Advertisement
ZaxonXP45
Jan 18th, 2023 (edited)
94
0
Never
This is comment for paste BASH GAWK File Count with Columns
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/perl
  2. # Perl version (no external tools needed). I did not make one liner for clarity.
  3.  
  4. use warnings;
  5. use strict;
  6.  
  7. use File::Find;
  8. use File::Spec;
  9.  
  10. my (%data, %data2, $total, $idx, $dl); my $max = 0;
  11.  
  12. sub findfiles() {
  13.     return if (! -f);
  14.  
  15.     my($vol,$dir,$file) = File::Spec->splitpath($File::Find::name);
  16.  
  17.     $data{$dir} = exists $data{$dir} ? $data{$dir}+1 :  1;
  18.     $total++;
  19. }
  20.  
  21. finddepth( { wanted => \&findfiles }, Cwd::getcwd);
  22.  
  23. foreach my $key (keys %data) {
  24.     my $dir = $key;
  25.     $dir =~ s#.*/(.+)/$#$1#;
  26.     $dl = length($dir);
  27.     $max = $dl > $max ? $dl : $max;
  28. }
  29.  
  30. foreach my $i (sort { $data{$a} <=> $data{$b} } keys %data) {
  31.     my $cnt = $data{$i};
  32.     $i =~ s#.*/(.+)/$#$1#;
  33.     printf("%*d) %-*s %d\n", length(keys %data), ++$idx, $max, $i , $cnt);
  34. }
  35. printf("%s\nTotal number of files: %d\n", "-" x 30, $total);
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement