first two results from ls command

Fidel picture Fidel · May 9, 2012 · Viewed 83.2k times · Source

I am using ls -l -t to get a list of files in a directory ordered by time.

I would like to limit the search result to the top 2 files in the list.
Is this possible?
I've tried with grep and I struggled.

Answer

dag picture dag · May 9, 2012

You can pipe it into head:

ls -l -t | head -3

Will give you top 3 lines (2 files and the total).

This will just give you the first 2 lines of files, skipping the size line:

ls -l -t | tail -n +2 | head -2

tail strips the first line, then head outputs the next 2 lines.