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/
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/
2013-08-03 04:50:02.000000000 -0400     12496901 ./TitaniumBackupS3/
2013-08-03 04:49:49.000000000 -0400      6786644 ./TitaniumBackupS3/
2013-08-03 04:49:39.000000000 -0400      6722444 ./TitaniumBackupS3/
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/

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

These Might Interest You

  • Sorts by latest modified files by looking to current directory and all subdirectories Show Sample Output

    find . -name '*pdf*' -print0 | xargs -0 ls -lt | head -20
    fuats · 2013-10-03 21:58:51 0
  • Ever wanted to find the most recently modified files, but couldn't remember exactly where they were in a project directory with many subdirectories? The "find" command, using a combination of "-mtime -N" and "-depth -D" can be used to find those files. If your directory structure isn't very deep, just omit the "-depth -D", but if your directory structure is very deep, then you can limit the depth of the traversal using "-depth -D", where "D" is the maximum number of directory levels to descend. Show Sample Output

    find . -type f -depth -3 -mtime -5
    totoro · 2009-03-25 19:54:06 0

  • 0
    find . -type f -printf "%T@ %Tc %p\n" |sort -n |cut -d' ' -f2- |tail -n20
    guziec · 2012-12-04 14:23:55 0
  • zsh globbing and glob qualifier: '**/*' = recursive om = ouput by modification (last access) [1,20] = twenty files. The '-t' switch is provided to ls so that the files are ordered with the most recent at the top. For a more 'find' like output the following can be used. print -rl **/*(om[1,20])

    ls -tl **/*(om[1,20])
    khayyam · 2013-03-24 00:14:03 0

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 · 254 weeks 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 · 249 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: