Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Let's say I have a command

ls -Bgclt /somwhere/in/the/past

How do I limit the output to show me only first 2 files? (except for having only 2 files in that directory)

share|improve this question
up vote 24 down vote accepted

Simple - you pipe the output through head:

ls -Bgclt /somwhere/in/the/past | head -n 3

You use -n 3 instead of -n 2 because of the 'total' line at the top of the ls output.

share|improve this answer
Great, thanx! – Denys S. Mar 30 '11 at 13:06
Bangin'! Thanks! – Josh Pinter Jan 15 '15 at 1:19

If you are really picky and only want to see the name of those two lines (that is, you want to exclude that first line with the word 'total' at the top) you can try

ls -Bgclt /somwhere/in/the/past | head -n 3 | tail -n 2
share|improve this answer
I can exclude it by grep as well. – Denys S. Mar 31 '11 at 14:07
@den-javamaniac : True, I was considering that as well. Only catch is if one of the files you list happen to contain the string you base the grep exclusion on. How likely that is to happen then is another matter. – IllvilJa Mar 31 '11 at 15:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .