fabrizio_start_perl has asked for the wisdom of the Perl Monks concerning the following question:

Hi Support, I would like to print the latest file that has been updated into a specific directory (Windows and Linux node). In my case i known only the prefix name of the file and not the full name. Example: folder /tmp/fab/tomcat/applicazione1/logs logfile -rw-r--r-- 1 root root 60 Oct 30 14:43 catalina.out -rw-r--r-- 1 root root 0 Oct 30 17:09 catalina.20143010.log and i need to print catalina.20143010.log one. Regards, Thanks Fabrizio

Replies are listed 'Best First'.
Re: print last file updated into directory
by toolic (Bishop) on Oct 30, 2014 at 17:18 UTC
    Prints the most recently modified "catalina." file in the current directory:
    use warnings; use strict; print((sort { -M $a <=> -M $b } grep { -f } glob 'catalina.*')[0], "\n +");

      Thanks a lot for all answer!
Re: print last file updated into directory
by perlron (Pilgrim) on Oct 30, 2014 at 17:09 UTC

    perldoc -f readdir and opendir to gain an understanding of the calls involved. this code and other exhaustive examples for directory listing are from perl meme website
    #!/usr/bin/perl use strict; use warnings; my $dir = '/tmp'; opendir(DIR, $dir) or die $!; while (my $file = readdir(DIR)) { # We only want files next unless (-f "$dir/$file"); # Use a regular expression to find files ending in .txt next unless ($file =~ m/\.txt$/); print "$file\n"; } closedir(DIR); exit 0;

    The temporal difficulty with perl is u need to know C well to know the awesome.else u just keep *using* it and writing inefficient code

      readdir is low-level

      use Path::Tiny qw/ path /; for my $kid ( path( $dir )->children( qr/\.txt$/ ) ){ print "$kid\n" if -f $kid; }

        Hello Anonymous Monk,
        By low level , is it that it is not an efficient way to traverse directory structures ?
        was just reading up about Path::Tiny on CPAN. It says it does not try to work in non-Windows and non-linux mode.
        also i see it has quite a few dependencies and wondering if it was pure perl or not, which might be a deciding factor for me ?
        I ran the following command to check the above
        perl -MPath::Tiny -MDynaLoader -E 'say for sort $@DynaLoader::dl_modul +es;'
        which gave me the output
        Cwd File::Glob

        The temporal difficulty with perl is u need to know C well to know the awesome.else u just keep *using* it and writing inefficient code
Re: print last file updated into directory
by jellisii2 (Hermit) on Oct 30, 2014 at 18:16 UTC
    depending on how your infrastructure is set up, doing this in Java may be more advantageous, since you're running Tomcat and (I assume) code with it already...