23

I'd like to know how to

cat my-verylong-file

and show it from the top instead of the end. Like the man command does.

muru
  • 197,895
  • 55
  • 485
  • 740
IDK
  • 593
  • 5
    For completeness, there is also the more command... but since more is less capable than less, it's very true that "less is more". Finally there is also pg for "page". You can also use these commands with pipes - eg. ls -l | less to break a very long file-listing into multiple pages. And you could even use cat my-verylong-file | less. – Baard Kopperud Sep 17 '17 at 14:40
  • Also worth pointing out that "By default, man uses /usr/bin/less -is." (from man man). – Digital Trauma Sep 18 '17 at 04:33

2 Answers2

50

Use less, which will show the file from the top, allowing you to scroll through it, just like man:

less my-verylong-file
  • Press q to exit.

If you're just interested in seeing the n first lines of a file, head may be an alternative:

head -n 25 filename

will show the 25 first lines of the file.

Same thing for the n last lines of a file with tail:

tail -n 25 filename
Zanna
  • 70,465
vidarlo
  • 22,691
  • 2
    less could be used also to scroll very-long outputs of commands. Here is presented an example: ls -laR --color=always ~/ | less -R. – pa4080 Sep 17 '17 at 10:32
  • 2
    And the command name "less" is a pun on the command name "more" (which it more or less replaces) which in turn was named from the "--More" prompt it gave. Under MacOS it appears that more is the same binary as less so the metamorphosis is complete :) – Thorbjørn Ravn Andersen Sep 18 '17 at 06:25
  • 5
    So, More or less the same thing? – Ryan Leach Sep 18 '17 at 07:21
  • 1
    @pa4080: you can set up less so -R is enabled by default. See my answer. – Peter Cordes Sep 18 '17 at 07:49
  • 1
    @RyanTheLeach more and less: the same thing :) – chepner Sep 18 '17 at 15:28
  • more and less are not really the same thing. more can only go forward, but less can also go backward, which is why it is named the way it is. The more and less commands can be the same binary, which then examines the name by which it was invoked to determine whether to allow going back. – Monty Harder Sep 18 '17 at 15:41
12

man uses less(1) as a pager by default. Use it instead of cat. See also @vidarlo's answer.

How to customize less to make it even better:

I like to alias m=less, so it's just a single-letter command, because I type it all the time. Putting a |m at the end of anything pipes it into a pager.

You could put options like -iMRj5X in the alias (e.g. alias m='less -iMRX), but I do that with my ~/.lesskey file. (See lesskey(1)).

  • -i: searches are case-insensitive (unless you use any capital letters)
  • -M: longer status line, showing line number and file-percentage
  • -R: allow some control-codes through, so you can pipe colorized commands into less.
  • -X: don't switch to the terminal emulator's "alternate" screen, so whatever you were looking at will still be there when you quit out of less. (great for man pages after you find the option you want, and want to look at it while typing it.)
  • -j5: searches put the target line at row 5 instead of the top of the screen. So you can see context on both sides of your search result. (Sometimes I change this interactively, by typing -j40 or something inside less, if it's most useful to see context before a search hit).

I also bind . to next-file, and , to prev-file, because the default bindings are two separate characters which are much slower to type: :n and :p.


This is my .lesskey:

$ cat .lesskey
. next-file
, prev-file
#env
LESS = iMRj5X

Run lesskey to "compile" it into a ~/.less.

This probably mattered more 20 years ago, but less reads that binary file instead of parsing a text config file every time it starts.

Peter Cordes
  • 2,197
  • @Dan: Added a line at the top so this answer can stand on its own as an answer to the question. I thought that leaving that part implicit would be sufficient given the other answer, but if I have no objection to making it explicit if it was bothering you. – Peter Cordes Sep 18 '17 at 09:10