10

Can I get the specified number lines at a time by using the less command? I want just ex. show 20 lines even if my screen allows more.

7
  • when less it started the line numbers displayed, and the total number of lines is reported at bottom of screen. Commented Dec 19, 2013 at 12:53
  • Please explain exactly what you are trying to do, it does not seem like less is the correct tool for the job. Commented Dec 19, 2013 at 13:02
  • 1
    ls | head 40 | tail 10 will show entries 31-40, but maybe I don't understand what you mean. Commented Dec 19, 2013 at 13:12
  • @Anthon I am not new SE. I am here from almost 2 years. Thank you. & by the way I am giving votes also. Commented Dec 19, 2013 at 13:14
  • 3
    If you think it is worth 20 downvotes, either fix the question or delete it. Commented Dec 19, 2013 at 16:20

3 Answers 3

7

less works with screens of text. The "screen" is the full size of the terminal.

less --window=n can tell less to only use so many rows at a time. That being said the option is not always available.

see man less

If you only want "some" output try tail -n 20 /file.txt for the last 20 lines, or I personally use head -n 20 | tail -n 10 to get the middle 10 lines.

4
  • I am sure there is a way to tell the terminal to only be so many rows, but that seems like a lot of work. Commented Dec 19, 2013 at 13:01
  • 4
    But less --window=N (or less -zN equivalently) doesn't (unfortunately) limit the number of strings initially shown on screen. It only affects how many lines it scrolls. What if I want to hide lines after N initially, and then scroll by N? LINES environment variable unfortunately has no effect (as per the man page, because it gets this info directly from the terminal device). Commented Nov 10, 2019 at 1:39
  • correct. You can change the window size but it still loads the entire house Commented Nov 10, 2019 at 4:26
  • For clarification: head -n 20 cuts everything after line 20 and tail -n 10 cuts from the remaining 20 lines everything before line 11 (including line nr. 10). So you end up with an absolute of 10 lines: Lines 11,12,...,20 Commented Jan 12, 2023 at 8:59
2

Display a file from line number X:

less +X filename 

Use the -N option to output line numbers

e.g less +15000 -N filename 

displays from line number 15000 with line numbers displayed

4
  • thank you , how about output from terminal & not from a file ? can i still use this ? Commented Dec 19, 2013 at 13:05
  • yes works fine from a terminal. e.g. cat filename | less +15000 -N Commented Dec 19, 2013 at 13:07
  • terminal output from some commands. Not from a file. Commented Dec 19, 2013 at 13:10
  • yes the same principle applies ps | less +2 Commented Dec 19, 2013 at 13:13
1

from less manual, to scroll n lines at a time, but shows a whole screen-full.

-[z]n or --window=n

Changes the default scrolling window size to n lines. The default is one screenful. The z and w commands can also be used to change the window size. The "z" may be omitted for compatibility with some versions of more. If the number n is negative, it indicates n lines less than the current screen size. For example, if the screen is 24 lines, -z-4 sets the scrolling window to 20 lines. If the screen is resized to 40 lines, the scrolling window automatically changes to 36 lines.

1
  • 4
    But less --window=N (or less -zN equivalently) doesn't (unfortunately) limit the number of strings initially shown on screen. It only affects how many lines it scrolls. What if I want to hide lines after N initially, and then scroll by N? LINES environment variable unfortunately has no effect (as per the man page, because it gets this info directly from the terminal device). Commented Nov 10, 2019 at 1:39

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.