Find last modified files in a directory and its subdirectories

find . -type f -print0 | xargs -0 stat -c'%Y :%y %12s %n' | sort -nr | cut -d: -f2- | head
Goes through all files in the directory specified, uses `stat` to print out last modification time, then sorts numerically in reverse, then uses cut to remove the modified epoch timestamp and finally head to only output the last 10 modified files. Note that on a Mac `stat` won't work like this, you'll need to use either: find . -type f -print0 | xargs -0 stat -f '%m%t%Sm %12z %N' | sort -nr | cut -f2- | head or alternatively do a `brew install coreutils` and then replace `stat` with `gstat` in the original command.
Sample Output
2013-08-03 04:54:27.000000000 -0400    172595617 ./Camera Uploads/2013-08-02 17.51.09.mp4
2013-08-03 04:51:20.000000000 -0400     28634837 ./TitaniumBackupS3/flipboard.app-20130803-060750.tar.gz
2013-08-03 04:50:50.000000000 -0400     16784821 ./TitaniumBackupS3/mobi.beyondpod-20130803-060106.tar.gz
2013-08-03 04:50:27.000000000 -0400     13551157 ./TitaniumBackupS3/mobi.mgeek.TunnyBrowser-20130803-060458.tar.gz
2013-08-03 04:50:13.000000000 -0400     13119509 ./TitaniumBackupS3/com.google.android.apps.maps-20130803-061440.tar.gz
2013-08-03 04:50:02.000000000 -0400     12496901 ./TitaniumBackupS3/com.android.providers.contacts-20130803-060316.tar.gz
2013-08-03 04:49:49.000000000 -0400      6786644 ./TitaniumBackupS3/com.shazam.android-107ba65d65701352ac124ba6339653f0.apk.gz
2013-08-03 04:49:39.000000000 -0400      6722444 ./TitaniumBackupS3/com.linkedin.android-c5a39964884b4a1a882a74e4e4eb801d.apk.gz
2013-08-03 04:49:38.000000000 -0400      6385957 ./TitaniumBackupS3/com.facebook.katana-20130803-060712.tar.gz
2013-08-03 04:49:27.000000000 -0400      6022831 ./TitaniumBackupS3/com.mxtech.videoplayer.ad-7c3e3cc2898543fa7e4c4b7a3e325b60.apk.gz

5
By: HerbCSO
2013-08-03 09:53:46

What Others Think

The find command has a very rich syntax, and its -printf option gives you a total control for its output. Here the stat command is useless, you can produce a quite similar result with: find . -type f -printf "%A+ %p\n" | sort -nr | head
jld · 271 weeks and 4 days ago
@jld, good tip, thanks! -printf is unknown to the Mac find (*sigh*), but the coreutils and gfind will work around that. Also, to be technically accurate, in order to make it the same as my command, you should substitute %A with %T to get the last modified time, i.e.: find . -type f -printf "%T+ %p\n" | sort -nr | head
HerbCSO · 266 weeks and 4 days 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?

commandlinefu.com 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.

» 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: