An overview of file paging applications
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.
MoreMostThe 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.
Lessmost 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 mostOnce installed you can use it in the expected fashion:
most /etc/passwdScrolling 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 mostA 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/passwdOr 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 lessOnce 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].txtThis 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.logThen 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 --helpIt 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.
[ Parent | Reply to this comment ]
[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 ]
--------
Felipe Sateler
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
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 ]
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 ]
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 ]
so instead of downloading the file, just
export LESSOPEN="|lesspipe %s"
[ Parent | Reply to this comment ]
--
"It's Not Magic, It's Work"
Adam
[ Parent | Reply to this comment ]
[ Send Message | 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 ]
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 ]
OpenSSH has been included and enabled by default in Sun Solaris for several years now.
[ Parent | Reply to this comment ]
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 ]
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 ]
tail has a "follow" option as well:
tail -f filename
[ Parent | Reply to this comment ]
[ Send Message | 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 ]
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
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 ]
watch tail -n 20 access.log
(it periodicly prints out last n lines form access log)
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
May be somebody know how this do?
[ Parent | Reply to this comment ]
May be somebody know how this do? ;-)
[ Parent | Reply to this comment ]
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 ]