Bash command to cut output after x number of lines

45,482

Use head:

ls -l | head -n 15

10 lines is the default. Read the head man page for more options.

(older versions of head also support usage without the explicit -n as in head -15)

Share:
45,482

Related videos on Youtube

simon
Author by

simon

Updated on September 17, 2022

Comments

  • simon
    simon over 1 year

    I'm looking for a bash command, which I can use to limit the number of lines of a file or another command output. E.g.

    ls -thor | limit 10
    

    would limit the output of the ls command to 10 lines (in the example, the command "limit" is naturally a imaginary command, whose equivalent I'm looking for). Is there such command, or a related solution?

  • David Yates
    David Yates over 14 years
    beat me to it :)
  • simon
    simon over 14 years
    Thx. I should have figured it out, it's obvious when you know that there is a "tail" command.. :)
  • Admin
    Admin over 14 years
    Syntax -integer is (at least in GNU version of head/tail discouraged - you should be using -n 15)
  • Doug Harris
    Doug Harris over 14 years
    I didn't know that. Old habits die hard. I'll have to try to learn that.