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.
3 Answers
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.
- 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.coteyr– coteyr2013-12-19 13:01:46 +00:00Commented Dec 19, 2013 at 13:01
- 4But
less --window=N(orless -zNequivalently) 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?LINESenvironment variable unfortunately has no effect (as per the man page, because it gets this info directly from the terminal device).imz -- Ivan Zakharyaschev– imz -- Ivan Zakharyaschev2019-11-10 01:39:16 +00:00Commented Nov 10, 2019 at 1:39 - correct. You can change the window size but it still loads the entire housecoteyr– coteyr2019-11-10 04:26:40 +00:00Commented 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,...,20Valerij Dobler– Valerij Dobler2023-01-12 08:59:17 +00:00Commented Jan 12, 2023 at 8:59
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
- thank you , how about output from terminal & not from a file ? can i still use this ?Raja G– Raja G2013-12-19 13:05:17 +00:00Commented Dec 19, 2013 at 13:05
- yes works fine from a terminal. e.g.
cat filename | less +15000 -Nsuspectus– suspectus2013-12-19 13:07:28 +00:00Commented Dec 19, 2013 at 13:07 - terminal output from some commands. Not from a file.Raja G– Raja G2013-12-19 13:10:48 +00:00Commented Dec 19, 2013 at 13:10
- yes the same principle applies
ps | less +2suspectus– suspectus2013-12-19 13:13:49 +00:00Commented Dec 19, 2013 at 13:13
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.
- 4But
less --window=N(orless -zNequivalently) 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?LINESenvironment variable unfortunately has no effect (as per the man page, because it gets this info directly from the terminal device).imz -- Ivan Zakharyaschev– imz -- Ivan Zakharyaschev2019-11-10 01:39:46 +00:00Commented Nov 10, 2019 at 1:39
ls | head 40 | tail 10will show entries 31-40, but maybe I don't understand what you mean.