How do I limit the number of displayed lines through ls?

29,213

Solution 1

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.

Solution 2

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:
29,213

Related videos on Youtube

Denys S.
Author by

Denys S.

Updated on September 18, 2022

Comments

  • Denys S.
    Denys S. over 1 year

    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)

  • Denys S.
    Denys S. about 13 years
    I can exclude it by grep as well.
  • IllvilJa
    IllvilJa about 13 years
    @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.