How to use `cat` to see the top of a very long file?

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

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.