Guest User

Untitled

a guest
Jul 16th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use File::Find;
  5. use File::Stat qw/:stat/;
  6.  
  7. sub _process_directories {
  8. # return only binary files
  9. return grep { -B }
  10. sort( @_ );
  11. }
  12.  
  13. sub _postprocess_directories {
  14. }
  15.  
  16. sub _process_files {
  17. return unless -f;
  18. # generate a stat object for each file
  19. # or return undef no empty hashref
  20. my @stat_obj = map({ $sb = stat $_;
  21. $_ = (defined $sb) ? {
  22. name => $_,
  23. owner => $sb->uid(),
  24. group => $sb->gid(),
  25. size => $sb->size() / 1024,
  26. mtime => $sb->mtime(),
  27. }
  28. : undef
  29. } $_);
  30. # generate the output from the stat object
  31. print map( {
  32. "$_:".$stat_obj[0]->{$_}.' '
  33. }
  34. #grep { !/time/ }
  35. keys(%{$stat_obj[0]})),"\n";
  36. }
  37.  
  38. my %options = (
  39. wanted => \&_process_files,
  40. preprocess => \&_process_directories,
  41. #postprocess => \&_postprocess_directories,
  42. bydepth => 1,
  43. follow_skip => 2,
  44. no_chdir => 0,
  45. );
  46. finddepth(\%options,@ARGV);
  47. =head1 About This Script
  48.  
  49. I read 'Perl Best Practices' - Damian Conway
  50.  
  51. In the book he talked a lot about instinctual coding versus thoughtful coding.
  52. I realized I code instinctually and this was my attempt at thoughtful code.
  53. The book stressed the cost and unneccessary need of typical iteratives and suggested
  54. deploying grep && map as a substitute to 'instinctual for' loops.
  55.  
  56. This was my first attempt at using compound iteratives via map && grep.
  57.  
  58. =head1 IDEA
  59.  
  60. Recursively crawl a directory and report on some `file stat` metadata.
  61. This could help for troubleshooting directories which contain large amounts of files.
  62. Or easily be adapted for other filesystem crawling purposes.
  63.  
  64. =head2 Meat and Potatos
  65.  
  66. =over 8
  67.  
  68. Simply put File::Find does all the hard work, I use its preprocess and work subroutines to report on
  69. filesystem information with a compound print statements using map, grep and sort.
  70.  
  71. =back
  72.  
  73. =head1 BUGS
  74.  
  75. This script probably has bugs.
  76. It was a learning exercise.
  77. Do not use this for any sort of production.
Add Comment
Please, Sign In to add comment