Posted by Steve on Fri 16 Sep 2005 at 14:28
Every now and again I like to spend a while becoming really familiar with the various tools which I use frequently. This week I spent several hours experimenting with the various paging applications available to Debian users. Here is my guide to the common pagers, and what they can do.
A pager is something that most administrators use frequently, to view files, read the results of long command output, and more.More
The standard Unix pager is /bin/more, which is contained in the Debian util-linux package. This package is required upon Debian systems, so more will always be available to you, short of accident!
more is a fairly basic program. It allows you to view a file, or any input piped to it in "pages" (i.e. a single screenful at once). That explains why this and similar programs are called pagers as they show you information in single pages.
One of the reasons that more isn't used more often is that in many Unix installations you can only scroll forward through the file - you cannot scroll backwards, nor can you perform searches. This applies to the program of the same name which is installed in Microsoft Windows for example.
The version of more in Debian doesn't suffer from these problems, but if you use other platforms frequently you need to look for an alternative. The commonest program for paging is now GNU less.
Using more is very straightforward:
- To view a file run:
- "more fileName", eg. "more /var/lib/dpkg/available".
- To view the output of a command in a paged manner run:
- "command | more", eg. "ps -aux | more".
Pressing the "?" will provide you with a minimal help interface, showing the available commands available to you.
The most useful are:
- space to scroll forwards.
- b to scroll backwards - if available.
- / to search forwards for a particular line - if available.
- v to open the file you're currently looking at in an editor, at the current position.
There is no ability to search backwards, and more will exit immediately when it reaches the end of the file - this is a big irritation which you cannot fix.
most differs from other pagers as it allows you to have more than one file visible and open at the same time.
This is a hugely useful ability if you're trying to view things easily - although I guess if you do it as a means of editing/copy + pasting you're probably better off using an editor which allows multiple windows (or buffers), such as Emacs, or Vim.
Since we've mentioned Emacs already it is worth noting that most uses Emacs key bindings natively. If you're a fan of Emacs already this is a good thing. If you're not you'll likely dislike it.
Installing most is very simple. Run, as root, the following command:apt-get install most
Once installed you can use it in the expected fashion:most /etc/passwd
Scrolling differs in the keystrokes we've seen so far in more which are largely shared in less too. Pressing h for help will give you all the details. But a short summery is:
- space - Scroll down.
- u - Scroll up.
- t - Top of file.
- b - Bottom of file.
- j - Jump to line.
- / - Search forwards.
- ? - Search backwards.
(Despite the fact that the keystrokes are displayed in upper-case in the help they work in lower-case, so you don't need to use the shift key.)
Once you've loaded one file you can split the window via the Ctrl-x 2 keystroke, and swap between the two windows with Ctrl-x o.
New files can be examined by loading them with Ctrl-x Ctrl-f.
(To use these multi-key commands simply press "Ctrl + x", then release both and press "Ctrl+f" at the same time. It might take a bit of time getting used to initially; but if you're familiar with GNU Emacs it will be second nature by now!)
The only obvious shortcoming to the most application is that you cannot use "TAB completion" to load files. If you don't know the full filename you wish to load then you're out of luck..
There are many available command line options which you can use. These are all listed in the manpage which you can read via:man most
A useful option is the ability to start viewing a file at a given line, or at a given string wherever it occurs in a file. The first of these options is also available under more, but the latter isn't.
For example to view the file /etc/passwd beginning at line 20 run:most +20 /etc/passwd
Or to start viewing the file at the first mention of Steve run:most +/Steve /etc/passwd
Less is described as "the opposite of more" in its manpage, which also describes some of the advantages:
Less is a program similar to more, but which allows backward movement in the file as well as forward movement.
Also, less does not have to read the entire input file before starting, so with large input files it starts up faster than text editors like vi.
If you don't have it installed already you can get it by running, as root:apt-get install less
Once installed basic usage is very simple:
- To view a file run:
- "less fileName", eg. "less /var/lib/dpkg/available".
- To view the output of a command in a paged manner run:
- "command | less", eg. "ps -aux | less".
Once you have less running you can do many things:
- Scroll forwards through the file, using the space key.
- Scroll backwards through the file, using the "b" key.
- (Often the arrow keys "up" and "down" will work for you, along with the "Page Up" and "Page Down" keys.)
- Search forwards by typing "/".
- Search backwards from the current position via the "?" key.
- Edit the currently displayed file via the "v" key.
- Go to the end of the file with "G", just like in vi.
One nice feature of less is that you can run it with multiple files, and move between them. Assuming you have three files "1.txt", "2.txt" and "3.txt" you could view them all by running:less [1-3].txt
This will load all three files, and allow you to cycle between them by pressing ":n" to view the next file or :p to view the previous file.
The ability to move between files also includes the ability to move between new files you've loaded. If you are viewing one file and wish to examine another type ":e" and you will be prompted the filename of the new file to examine. (This prompt includes support for TAB completion.)
less is also good for watching logfiles, as it will allow you to "follow" new input. If you've got an Apache webserver running you can keep an eye upon the most recent entries in its logfile by running:less /var/log/apache/access.log
Then press "F" to jump to the end of the file and automatically update the screen when new lines are added. (Yes that is a capital F - lower-case f moves forward one line!)
To cancel the following press Ctrl+c.
There are a couple more neat tricks you can do with less, such as toggling the display of long lines - simply type "--chop-long-lines" and the lines will no longer wrap. (You can take advantage of the smart completion to avoid having to type this out fully). Run it again to undo the chopping.
There are a lot of available options to experiment with, for the full details you can either read the manpage, or run:less --help
It is probably worth noting that if you execute the following command you can suddenly use less to examine tar files, compressed files, and other non-text items without any special treatment:eval $(lessfile)
This even allows you to run less upon a binary .deb file:skx@mystery:~$ less mousetrap_0.8-1_i386.deb mousetrap_0.8-1_i386.deb: new debian package, version 2.0. size 281518 bytes: control archive= 1138 bytes. 430 bytes, 13 lines control 1167 bytes, 16 lines md5sums 185 bytes, 7 lines * postinst #!/bin/sh 160 bytes, 5 lines * postrm #!/bin/sh Package: mousetrap Version: 0.8-1 Section: games Priority: optional Architecture: i386 Depends: debconf (>= 1.2.0), libsdl1.2debian, libsdl-gfx1.2, libsdl-mixer1.2 Installed-Size: 688 Maintainer: Steve Kemp Description: A simple game of ball chasing This is a simple game which the player moves his ball to chase fruit, and avoid the linear enemies. *** Contents: drwxr-xr-x root/root 0 2005-07-03 02:24:23 ./ drwxr-xr-x root/root 0 2005-07-03 02:24:20 ./usr/ drwxr-xr-x root/root 0 2005-07-03 02:24:23 ./usr/games/ -rwxr-xr-x root/root 19048 2005-07-03 02:24:23 ./usr/games/mousetrap drwxr-xr-x root/root 0 2005-07-03 02:24:23 ./usr/share/ ... ...
In conclusion the most feature rich pager available within Debian appears to be, in my opinion, GNU Less. But the other options may be worthy of investigation.