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.
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.
Use less
, which will show the file from the top, allowing you to scroll through it, just like man
:
less my-verylong-file
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
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
less
so -R
is enabled by default. See my answer.
– Peter Cordes
Sep 18 '17 at 07:49
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
man
uses less(1)
as a pager by default. Use it instead of cat
. See also @vidarlo's answer.
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 q
uit 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.
more
command... but sincemore
is less capable thanless
, it's very true that "less is more". Finally there is alsopg
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 usecat my-verylong-file | less
. – Baard Kopperud Sep 17 '17 at 14:40man man
). – Digital Trauma Sep 18 '17 at 04:33