How can I list (ls) the 5 last modified files in a directory?

Ryan picture Ryan · Mar 28, 2013 · Viewed 265.2k times · Source

I know ls -t will list all files by modified time. But how can I limit these results to only the last n files?

Answer

Paul Rubel picture Paul Rubel · Mar 28, 2013

Try using head or tail. If you want the 5 most-recently modified files:

ls -1t | head -5

The -1 (that's a one) says one file per line and the head says take the first 5 entries.

If you want the last 5 try

ls -1t | tail -5