By default, the code below searches for a subtree under the current working directory. You can also specify a few more subtrees to search on the command line.
#! /usr/bin/env perl use strict; use warnings; use File::Find; my($newest_mtime,$path); sub remember_newest { return if -l || !-f _; my $mtime = (stat _)[9]; ($newest_mtime,$path) = ($mtime,$File::Find::name) if !defined $newest_mtime || $mtime > $newest_mtime; } @ARGV = (".") unless @ARGV; for (@ARGV) { if (-d) { find \&remember_newest, @ARGV; } else { warn "$0: $_ is not a directory.\n"; } } if (defined $path) { print scalar(localtime $newest_mtime), "\t", $path, "\n"; } else { warn "$0: no files processed.\n"; exit 1; }
As written, the code does not follow symbolic links. If you specify a symbolic link on the command line, you will see the output
$ ./find-newest ~ / link-to-directory
./find-newest: no files processed.
With bash, you need to add a trailing slash to force dereferencing.
$ ./find-newest ~ / link-to-directory /
Thu Jan 1 00:00:00 1970 hello-world
source share