How to find newest files in a directory, including subdirectories?

12

2

I'm looking for a way in Windows to list all files in a given directory and its subdirectories, sorted so that the newest files are on top. Is there any easy way to do this? I'd also settle for a Perl script.

Kip

Posted 2010-02-15T20:44:09.713

Reputation: 4 275

Answers

3

If you're still interested in a scripted solution, I've quickly thrown something together which will display files from all directories together:

#!C:/Perl/bin/perl.exe
use File::Find;

my %files = ();

sub process
{
    $files{$_} = (stat($_))[9] unless -d $_;
}

find (\&process, $ARGV[0]);

foreach my $key(sort {$files{$b} <=> $files{$a}} keys %files)
{
    print "$key\n";
}

Files are displayed newest to oldest, showing only the file name. This way you can easily pipe the output of this script to another tool for processing without having to worry about stripping excess output.

Usage: perl find.pl <starting_directory>

John T

Posted 2010-02-15T20:44:09.713

Reputation: 149 037

thanks. i guess this should have been a stackoverflow question if it went in this direction. :) – Kip – 2010-02-15T22:54:02.083

1a few changes i made (for the help of anyone who'd happen to come across this): my $dir = ($#ARGV >= 0 ? $ARGV[0] : '.'); so that it will scan current dir if directory parameter isn't given, and to include the time that is printed: print strftime('%Y-%m-%d %H:%M:%S', localtime((stat($key))[9])) . " $key\n"; – Kip – 2010-02-15T22:55:22.017

1

@Kip, the mtime should be stored in $files{$key} if you want to use that for strftime when printing to avoid a second call to stat(). If you plan on piping it to another program, I've also modified it to display the full path of the filenames. You can grab that here: http://pastebin.ca/1797748

– John T – 2010-02-15T22:57:42.540

thanks, you're faster than i am. the stat command only worked for files in the current directory too, since you were only storing the last part of the filename. – Kip – 2010-02-15T23:05:44.473

7

Like this: dir /s /o:-d

SLaks

Posted 2010-02-15T20:44:09.713

Reputation: 7 596

1hmm... not quite ideal, this still lists each directory individually, but i can work with it – Kip – 2010-02-15T20:55:11.580

5

If you have cygwin or similar installed then find . -type f | xargs ls -tr will do the trick. You can almost certainly do similar with Microsoft's powershell.

David Spillett

Posted 2010-02-15T20:44:09.713

Reputation: 22 424

3

In case you're using Everything, just type the path and sort by Date Modified and you will get all files in this folder in that order, regardless the subdirectory.

Everything is freeware, a portable version is available.

Molly7244

Posted 2010-02-15T20:44:09.713

Reputation:

1

If you don't mind coding (though this takes the question into Stack Overflow territory) then there's the Directory and File classes in C#.

The GetFiles method has an overload that will return all filenames in all subdirectories. You can then loop over this list, calling GetLastWriteTime to get the modified date/time. Store the name and time in a dictionary, sort on the time and print out the filenames.

There's also the DirectoryInfo and FileInfo classes.

I should have added that all of this functionality is available via Powershell, so you don't have access to a full IDE.

ChrisF

Posted 2010-02-15T20:44:09.713

Reputation: 39 650

thanks. i am a developer though mainly in java. i was asking here hoping a shell option was available – Kip – 2010-02-15T22:45:37.577

@Kip - I realised last night after I logged out that I should have mentioned Powershell - http://technet.microsoft.com/en-us/scriptcenter/dd742419.aspx - it's a scripting language but you have access to the .NET runtime.

– ChrisF – 2010-02-16T08:31:07.310

1

If you have something like Git Bash or Cygwin installed, you should be able to use one of the answers from over here: https://stackoverflow.com/questions/4561895/how-to-recursively-find-the-latest-modified-file-in-a-directory

One that worked well for me is: find . -type f -printf '%TF %TT %p\n' | sort -r

You can also add | head -10 to the end of that to see just the 10 most recent, say.

peterflynn

Posted 2010-02-15T20:44:09.713

Reputation: 397