From: Robert Webb
Date: Tue, 28 Feb 2023 23:31:57 +0000 (UTC)
Since I often get side-tracked and forget what I was doing, I wrote a
script to find directories with recent (or otherwise selected) files
in them . . .
For grins, I wrote a variation on this in Perl. Enjoy,
-- Bob Rogers
http://www.rgrjr.com/
#!/usr/bin/perl
#
# [created, based on finddirs_by_latest_file.sh by Robert Webb, as posted to
# users@lists.opensuse.org. -- rgr, 28-Feb-23.]
#
use strict;
use warnings;
use Getopt::Long;
my $sort_by_dir;
my @find_opts;
GetOptions('sdir!' => \$sort_by_dir,
(map { ("$_=i" => \&find_opt); }
qw(amin atime ctime cmin gid inum links mtime mmin uid)),
(map { ("$_=s" => \&find_opt); }
qw(anewer cnewer fstype group ilname iname ipath iregex iwholename),
qw(lname name newer path perm regex samefile size user)))
or usage();
### Subroutines.
sub usage {
# Print a usage message & exit.
warn "Usage: $0 [ --sdir ] [ <find-opts> ] [ <target-dirs> ]\n";
exit(0);
}
sub find_opt {
# Store a "find" option after GetOptions has taken it off of @ARGV.
my ($opt, $value) = @_;
push(@find_opts, "-$opt" => $value);
}
### Main code.
unshift(@ARGV, '.')
unless @ARGV;
my $find_command = join(' ', 'find', @ARGV, @find_opts,
q{-type f -print});
open(my $find, "$find_command |")
or die "$0: Could not pipe from '$find_command': $!";
my %directory_file_count;
while (<$find>) {
next
if m@/[.](git|svn)/@;
s@[^/]+$@@;
$directory_file_count{$_}++;
}
my @dirs = keys(%directory_file_count);
for my $directory ($sort_by_dir ? sort(@dirs) : @dirs) {
printf("%7d %s\n", $directory_file_count{$directory}, $directory);
}