17

Probably been answered somewhere, but its difficult to frame the search phrase.

I am running a bash terminal window and some commands are too big to fit on the page (e.g. ps -A)

I vaguely recall a command line parameter / method that shows the command output page by page so I can scroll through the output, but I can't recall what it is.... any pointers?

6
  • 4
    ps -A | less or ps -A | more? There's also ps -A | vim - or ps | gvim - for the fans of vim. Jul 2, 2014 at 10:09
  • with shift up usually you can scroll up your terminal and read...
    – Hastur
    Jul 2, 2014 at 10:11
  • @CristianCiupitu That's the one! - less/more, thanks :) Jul 2, 2014 at 10:18
  • @Hastur Normally, yes, the terminal used has a buffer you can scroll, but I am using this on a really really basic terminal (seems to be from the 60's), there is no scroll/buffer. :( Jul 2, 2014 at 10:19
  • If you have less it's somehow better than more: you can scroll easily up and down, it's able to see if the file/stream is binary and to prompt before showing it (In this case you avoid to mess the character set of your screen with an uncontrolled binary text) :-).
    – Hastur
    Jul 2, 2014 at 10:27

3 Answers 3

14

For commands I use often, I generally set up a function in my .bashrc to make them paginate if longer than a screen.

Like your example: (ps -A)

function ps { command ps "$@" |& less -F; }

This replaces ps with a function, named ps, which calls the original ps command with whatever arguments given on the command line, then pipes the output (stdout and stderr, using the |& pipe) into less -F, which pauses if there's more than a screen-full, but exits immediately if it's less than a screen-full.

VERY handy, doesn't interfere with anything I've worked with so far, and is just cool!

You can even add oft-used options into the command/functions too:

function nm { command nm --demangle "$@" |& less -F; }

This makes nm always demangle C++ symbols. AND paginates the output. Yay!

I'm running Debian, so I use the apt-cache command quite often, search and show mostly. This function causes those particular options to paginate, search output is sorted, and everything paginates:

function apt-cache { case  "$1" in "search") command apt-cache "$@" | sort | less -F;; *) command apt-cache "$@" | less -F;; esac; }

If the command is 'search', sort the output, then paginate with less -F, but if command is anything else, just paginate, without sorting.

Occasionally I forget I've got the functions, and I'll do something like:

apt-cache search gcc | less

The function doesn't interfere, everything works as expected, no harm either way.

Another little tweak, I use the same .bashrc on all my systems, so sometimes a utility might not be installed, so there's no need for the function. I make them conditional like this:

which apt-cache &>/dev/null && function apt-cache { case  "$1" in "search") command apt-cache "$@" |& sort | less -F;; *) command apt-cache "$@" |& less -F;; esac; }

This just uses the which command to determine if a program is available, if it isn't, it quietly fails and skips installing the function. Taa Daa!

2
  • 1
    Thank you for demonstrating command, which I've always needed. Now I can actually redefine the builtin to have a nice function wrapping the real builtin!
    – bgStack15
    Jul 2, 2014 at 11:38
  • +1 agree with bgStack... though I think for my issue, less will do :) Jul 2, 2014 at 14:16
10

The normal method is to pipe the output to "less".

ls -R / | less

q is the key to quit, just like a man page.

If the command may produce errors or other output to stderr you may want to direct that to the pipe as well.

ls -R 2>&1 | less

Any machine that has bash should have less as well. On old Linux machines the program was more, but that does just a page at a time, less will allow you to scroll as you wish.

1
  • 1
    "q is the key to quit, just like a man page". That's because the output of the man command is piped to a pager like less or more, but if no pager is configured or a different one if used, the q key might not work. Jul 2, 2014 at 10:49
2

Pipe the output to 'more'

output | more -d


Enter - > Scroll by line
Space - > Scroll by Page
q     - > Quit

Tested on rpm based OS.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .