What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again.

Delete that bloated snippets file you've been using and share your personal repository with the world. 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.

If you have a new feature suggestion or find a bug, please get in touch via http://commandlinefu.uservoice.com/

Get involved!

You can sign-in using OpenID credentials, or register a traditional username and password.

First-time OpenID users will be automatically assigned a username which can be changed after signing in.

Universal configuration monitoring and system of record for IT.

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.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

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:



2011-03-12 - Confoo 2011 presentation
Slides are available from the commandlinefu presentation at Confoo 2011: http://presentations.codeinthehole.com/confoo2011/
2011-01-04 - Moderation now required for new commands
To try and put and end to the spamming, new commands require moderation before they will appear on the site.
2010-12-27 - Apologies for not banning the trolls sooner
Have been away from the interwebs over Christmas. Will be more vigilant henceforth.
2010-09-24 - OAuth and pagination problems fixed
Apologies for the delay in getting Twitter's OAuth supported. Annoying pagination gremlin also fixed.

Top Tags



Find and display most recent files using find and perl

Terminal - 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
2009-09-21 22:11:16
User: drewk
Functions: find perl
Find and display most recent files using find and perl

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.


There are 2 alternatives - vote for the best!

Terminal - Alternatives

Know a better way?

If you can do better, submit your command here.

What others think

A quick google reveals that "9" means "access time" http://perldoc.perl.org/functions/stat.html . 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...

Comment by bwoodacre 288 weeks ago

I don't follow in this case.

Comment by drewk 288 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...

Comment by drewk 288 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.

Comment by drewk 288 weeks ago

Your point of view

You must be signed in to comment.