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:



May 19, 2015 - A Look At The New Commandlinefu
I've put together a short writeup on what kind of newness you can expect from the next iteration of clfu. Check it out here.
March 2, 2015 - New Management
I'm Jon, I'll be maintaining and improving clfu. Thanks to David for building such a great resource!

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 [email protected]=<>; 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 10 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 348 weeks and 4 days ago

I don't follow in this case.

Comment by drewk 348 weeks and 4 days 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 348 weeks and 4 days 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 348 weeks and 3 days ago

Your point of view

You must be signed in to comment.