An overview of file paging applications

Posted by Steve on Fri 16 Sep 2005 at 14:28

Tags: ,

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

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

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.

 

 


Posted by Anonymous (69.196.xx.xx) on Fri 16 Sep 2005 at 15:26
vim is my editor, and I often page things using vim so that I can work with the data that was output by another program.

something-wiht-lots-of-output | vim -R -

vim -R - is not fun to type, so I made a short hand:

function v() { $@ | vim -R - ; }

Now I can type:

v something-with-lots-of-output

... this is not for everyone, but if you already use vim as your editor, give this a try.

[ Parent | Reply to this comment ]

Posted by Anonymous (216.220.xx.xx) on Fri 16 Sep 2005 at 16:26
I think the forthcoming vim7 has a builtin "vimpager" command that you set your $PAGER variable to in your shell's rc file. I haven't used this yet, as I am not running the alpha version of the new vim, but it might be someting to watch for.

[ Parent | Reply to this comment ]

Posted by apeekaboo (85.226.xx.xx) on Fri 16 Sep 2005 at 20:18
You can extend this further to let vim handle output from man, thus giving you man pages with syntax highlighting and colorcoding:
[eek@apeekaboo [20:48:12] eek]$ cat .vimrc.more 
" source everything from ~/.vimrc
so $HOME/.vimrc

" remove blankspace on each side of 'CR' on the line below
" the formatting with angled brackets is destroyed when posting here...
nmap q :q< CR >

[eek@apeekaboo [20:59:27] eek]$ echo $PAGER
col -b | view -X -u ~/.vimrc.more -c 'set ft=man nomod nolist' -

[ Parent | Reply to this comment ]

Posted by fsateler (201.214.xx.xx) on Sat 17 Sep 2005 at 04:50
[ View Weblogs ]
I also like to use vim as a pager, but for another reason: vim has pretty colors, so it is nicer and easier to examine, eg a shell script with vim rather than less.
--------
Felipe Sateler

[ Parent | Reply to this comment ]

Posted by Anonymous (213.216.xx.xx) on Sun 25 Sep 2005 at 11:26
You can also run "view" instead of vi for read-only "pager".

[ Parent | Reply to this comment ]

Posted by Anonymous (4.229.xx.xx) on Mon 10 Oct 2005 at 08:03
One problem with using editors as pagers is that they have to read the whole file. None of the pagers described in this article have to do that. So a proper pager is much faster and needs much less memory than an editor.

Yes, I agree it would be nice to have a pager with vi keybindings and syntax highlighting. Also some sort of bookmarks would be great.

[ Parent | Reply to this comment ]

Posted by Anonymous (82.119.xx.xx) on Fri 16 Sep 2005 at 16:04
LESS:
Instead of --chop-long-lines you can use "less -S" or when less is running you can switch chopping on/off by typing "-S". And about those binary files... I have a file called lesspipe.sh, I don't know it's origin, I had it on my 1st slackware linux in 1998 and still copy it to new machines :)
It tries to view all types of binary files, you have just to add these lines to /etc/profile:
LESS=-MM
LESSOPEN="|lesspipe.sh %s"
export LESS LESSOPEN

Download it here www.marki-online.net/lesspipe.sh.txt

[ Parent | Reply to this comment ]

Posted by Anonymous (64.142.xx.xx) on Fri 16 Sep 2005 at 17:19
The difference between lesspipe and lessopen is discussed in /usr/share/doc/less/LESSOPEN. In short, lesspipe stuffs the data in a pipe while lessfile uses a temp file.

Thus you can get the functionality you're achiving with your personal arcana with 'eval $(lesspipe)'. I'm not sure that these are part of the gnu distribution though, so carrying arround your personal "baggage" may be to your benefit in the (very) long run.

- k8to

[ Parent | Reply to this comment ]

Posted by Anonymous (195.176.xx.xx) on Wed 2 Nov 2005 at 10:14
The debian less-package comes with a similar script installed at /usr/bin/lesspipe.

so instead of downloading the file, just

export LESSOPEN="|lesspipe %s"

[ Parent | Reply to this comment ]

Posted by ajt (84.12.xx.xx) on Fri 16 Sep 2005 at 17:32
[ View Weblogs ]
I do like less, when I have to work on the AIX boxen at work with their antique shells and command tools, less is one of the commands I miss. more is okay, but less is better.

--
"It's Not Magic, It's Work"
Adam

[ Parent | Reply to this comment ]

Posted by Steve (82.41.xx.xx) on Sun 18 Sep 2005 at 11:43
[ View Steve's Scratchpad | View Weblogs ]

I miss it more on Windows where I find myself typing "| less" without thinking.

(Thankfully there are ports available.)

less is one of the tools I install upon all the machines I touch if possible, along with other programs like sudo and screen.

Steve
--

[ Parent | Reply to this comment ]

Posted by ajt (84.12.xx.xx) on Sun 18 Sep 2005 at 12:18
[ View Weblogs ]

On Windows I use Cygwin to use the Windows system or PuTTY and connect to something more useful...

sudo and screen are most usful, essential tools. The people I've worked with, with a commercial Unix background tend to sneer at Linux, and the tools on it. Yet they use the most antique and primitive systems:

  • csh and sh for default shell
  • more for a pager
  • rlogin/telnet rather than SSH
  • CDE for Window Manager rather than something modern

I'm not advocating new for the sake of new, but sometimes newer programs have more features, or are faster, or are more secure, or are more modular/flexible.

I've also encountered a lot of "Not Invented Here". Because Linux comes with OpenSSH, they won't use it in Unix. Even though I point out OpenSSH is nothing to do with Linux, and it's just that pretty much every Linux has OpenSSH, they assume because it's in Linux: a) it is Linux and b) it is cr*p.

Basicaly if it's in Linux and their Unix, then the Linux version is a cheap copy. If something is not in their Unix but in Linux then it's cr*p, and they don't want it. If something is in their Unix but not in Linux, then that proves that Linux is cr*p.

There is a lot of misunderstanding of what Linux is and distributions are. You would think that people with a Unix background would know, but they seem to be as without a clue as Windows users... -- "It's Not Magic, It's Work" Adam

[ Parent | Reply to this comment ]

Posted by Anonymous (64.252.xx.xx) on Sun 18 Sep 2005 at 22:03
"Because Linux comes with OpenSSH, they won't use it in Unix."

OpenSSH has been included and enabled by default in Sun Solaris for several years now.

[ Parent | Reply to this comment ]

Posted by ajt (204.193.xx.xx) on Mon 19 Sep 2005 at 10:56
[ View Weblogs ]
I also believe that other commercial Unix also ship with a lot of "standard" open source tools. The one person I'm thinking of in particular still wouldn't use them, it's still just Linux cr*p - even though it's nothing to do with Linux.

I suppose we all get use to our current tool-kit, and can be reluctant to try something new. I was just surprised by the level of inflexibility in some Unix people.

--
"It's Not Magic, It's Work"
Adam

[ Parent | Reply to this comment ]

Posted by Anonymous (82.82.xx.xx) on Fri 16 Sep 2005 at 22:10
Hi,

I really like lesspipe/lessfile. However, I would like to activate it system-wide and also for login shells, so that I can also use it when I login to a console.

"eval $(/usr/bin/lesspipe)" works in .bashrc. Why does it work in /etc/profile? Any ideas on howto get this going for all users and also login shells.

[ Parent | Reply to this comment ]

Posted by Kellen (68.15.xx.xx) on Sat 17 Sep 2005 at 01:06
[ View Weblogs ]
re: watching logfiles

tail has a "follow" option as well:
tail -f filename

[ Parent | Reply to this comment ]

Posted by Steve (82.41.xx.xx) on Sun 18 Sep 2005 at 11:40
[ View Steve's Scratchpad | View Weblogs ]

Yes, many people use tail for this job - or one of the more dedicated applications such as apachetop - I just thought it was worth mentioning.

Steve
--

[ Parent | Reply to this comment ]

Posted by Anonymous (80.136.xx.xx) on Sat 1 Oct 2005 at 22:01
Yes, tail has a "follow" option, but if you cancel the "follow" while inside less, you're back in less and positioned at the point you've stopped it. This allows you to immediately page backwards, e.g. if stuff scrolled by too fast. try doing that in tail ;-)

[ Parent | Reply to this comment ]

Posted by Anonymous (204.228.xx.xx) on Wed 12 Apr 2006 at 22:49
Easy- you should be using 'screen' already, thus simply hit ctrl+a, [ and then ctrl+u to page up. Then [ or ESC when you're done to jump back to the most recent info 'followed' by tail.

[ Parent | Reply to this comment ]

Posted by elivs (202.0.xx.xx) on Mon 19 Sep 2005 at 09:14
I put this in all ~/.bashrc and /etc/bash.bashrc files. Its really usefull. My pager is by far my most used appliaction. Making it a one letter alias is great.

eg

ps waux | m
m /etc/apt/sources.list
ls -lha | m

This must save me dozens of keystrokes per day, possibly even hundreds!! I got shown this by a unix admin at varsity. He claimed to have saved hundreds of keystrokes per day for over 20 years!

Elivs

[ Parent | Reply to this comment ]

Posted by bl4z (193.95.xx.xx) on Mon 19 Sep 2005 at 11:11
combo i like most is

watch tail -n 20 access.log
(it periodicly prints out last n lines form access log)

[ Parent | Reply to this comment ]

Posted by Anonymous (82.68.xx.xx) on Mon 19 Sep 2005 at 13:06
Another usefull tool is zless which allows you to view the contents of zipped files in less, very useful for those README.gz files :)

[ Parent | Reply to this comment ]

Posted by Anonymous (131.111.xx.xx) on Mon 19 Sep 2005 at 15:59
most can read gzipped text files natively... very very handy for looking at archived log files.

[ Parent | Reply to this comment ]

Posted by Anonymous (130.127.xx.xx) on Mon 19 Sep 2005 at 17:15
Not sure why you or the parent find this interesting - standard less can read gzipped text files natively as well.

[ Parent | Reply to this comment ]

Posted by Anonymous (212.111.xx.xx) on Wed 21 Sep 2005 at 07:13
Interesting does less support mouse scrolling?
May be somebody know how this do?

[ Parent | Reply to this comment ]

Posted by Anonymous (139.168.xx.xx) on Sat 22 Jul 2006 at 17:05
Interesting does less support grammar checking?
May be somebody know how this do? ;-)

[ Parent | Reply to this comment ]

Sign In

Username:

Password:

[Register|Advanced]

 

Flattr

 

Current Poll

What do you use for configuration management?








( 684 votes ~ 10 comments )