Ryan Ryan - 3 months ago 17
Linux Question

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

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

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
Comments