Find and display most recent files using find and perl

find $HOME -type f -print0 | perl -0 -wn -e '@f=<>; foreach $file (@f){ (@el)=(stat($file)); push @el, $file; push @files,[ @el ];} @o=sort{$a->[9]<=>$b->[9]} @files; for $i (0..$#o){print scalar localtime($o[$i][9]), "\t$o[$i][-1]\n";}'|tail
This pipeline will find, sort and display all files based on mtime. This could be done with find | xargs, but the find | xargs pipeline will not produce correct results if the results of find are greater than xargs command line buffer. If the xargs buffer fills, xargs processes the find results in more than one batch which is not compatible with sorting. Note the "-print0" on find and "-0" switch for perl. This is the equivalent of using xargs. Don't you love perl? Note that this pipeline can be easily modified to any data produced by perl's stat operator. eg, you could sort on size, hard links, creation time, etc. Look at stat and just change the '9' to what you want. Changing the '9' to a '7' for example will sort by file size. A '3' sorts by number of links.... Use head and tail at the end of the pipeline to get oldest files or most recent. Use awk or perl -wnla for further processing. Since there is a tab between the two fields, it is very easy to process.
Sample Output
find $HOME -type f -print0 | perl -0 -wn -e '@f=<>; foreach $file (@f){ (@el)=(stat($file)); push @el, $file; push @files,[ @el ];} @o=sort{$a->[9]<=>$b->[9]} @files; for $i (0..$#o){print scalar localtime($o[$i][9]), "\t$o[$i][-1]\n";}'|tail
Mon Sep 21 19:24:47 2009	/Users/andrew/Library/Logs/Sync/mobilemesyncstats.plist
Mon Sep 21 19:24:47 2009	/Users/andrew/Library/Preferences/ByHost/
Mon Sep 21 19:24:48 2009	/Users/andrew/Library/Preferences/ByHost/
Mon Sep 21 19:24:54 2009	/Users/andrew/Library/Cookies/Cookies.plist
Mon Sep 21 19:25:03 2009	/Users/andrew/Library/Preferences/
Mon Sep 21 19:26:44 2009	/Users/andrew/Library/Mail/ Messages.imapmbox/Messages/311191.emlx
Mon Sep 21 19:26:47 2009	/Users/andrew/Library/Logs/Sync/syncservices.log
Mon Sep 21 19:26:55 2009	/Users/andrew/Library/Preferences/
Mon Sep 21 19:27:48 2009	/Users/andrew/Library/Mail/
Mon Sep 21 19:27:49 2009	/Users/andrew/Library/Mail/Envelope Index

By: drewk
2009-09-21 22:11:16

What Others Think

A quick google reveals that "9" means "access time" . To avoid the multiple batches problem in the shell, you could use find to do the stat()ing for you via -printf, and then simply pipe it into a sort (don't you love shell?): find $HOME -type f -print "%+\t%f\n" | sort -n this is a good approximation, although there's something different between find's notion of atime and perl's...I'm probably missing something...
bwoodacre · 673 weeks ago
I don't follow in this case.
drewk · 673 weeks ago
Sorry on previous comment. I was looking at the wrong comment... Perl arrays are zero based, so the 10th element of the stat return is array index 9... The command you gave find $HOME -type f -print "%+\t%f\n" | sort -n does not work on OS X. :-( When I get time, I will debug...
drewk · 673 weeks ago
Sometimes you Linux guys have all the fun. You have: find $HOME -type f -print "%+\t%f\n" | sort -n If think you meant: find $HOME -type f -printf "%+\t%f\n" | sort -n The printf option is not available on OS X (and I don't think on BSD). One could use: find $HOME -type f -exec printf "format" {} \; | sort -n but I have 350,000 files in $HOME, and therefor 350,000 subshells. Slow. On Linux with -printf available as an option for find, your solution works well -- I think. I do not have Linux to test.
drewk · 673 weeks ago
No doubt this pipeline is much helpful to display most recent files using find and perl.This table help me alot.You must such informative and helpful pipeline to display files.hoverboard for sale 100
Lucas45 · 120 weeks and 5 days ago ????? ??? ????? ????? SEO - ????? ???? ????? ????? ????? ??? ????? ????? SEO - ????? ???? ????? ????? ????? ??? ????? ????? SEO - ????? ???? ????? ????? ????? ??? ????? ????? SEO - ????? ???? ????? ????? ????? ??? ????? ????? SEO - ????? ???? ????? ????? ????? ??? ????? ????? SEO - ????? ???? ????? ????? ????? ??? ????? ???? SEO ????? ????? ????? ???? ????? ????? ??? ???? ???? ??????? Search Engine Optimization ??? ????? ??????????? ?? ????? ?????????? ????? ????? ?? ???? ???SEO
seofox · 27 weeks and 6 days ago
seofox · 27 weeks ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: