Unix Lesson - Displaying more with "less"
Wolfgang Friebel , IT / DS
To browse files under UNIX you can use the excellent viewer less, the better
alternative to "more". By making use of the environment variable
Most Linux distributions come already preconfigured with a filter lesspipe.sh or lessopen.sh that covers the most common situations. The input filter lesspipe.sh for less that I am going to describe understands many of the more common file formats. It is easily extendable for new formats. The preprocessor for less is written in a ksh compatible language (ksh, bash, zsh) as one of these is nearly always installed on UNIX systems and uses relatively few resources.
The design of the input filter is based on two main ideas. The recognition of the file format is not based on the file suffix. This method from the DOS world is error prone and keeping the suffix list up to date is a tedious job. UNIX comes with the file command that recognizes lots of formats. Up to date file descriptions are included in the tarball, so maintaining a list of file formats is therefore only a matter of obtaining a current version of the file package.
The second idea is to being able to call lesspipe.sh with a hierarchy of file names and to pull out finally the file at the bottom of the hierarchy. This would allow you to look at individual files contained in an archive which itself could be part of a still bigger archive.
As lesspipe.sh accepts only a single argument, a hierarchical list of file names has to be separated by a nonblank character. As the colon is rarely found in file names, it has been chosen as the separator character. At each stage in extracting files from such a hierarchy the file type is determined. This guarantees correct processing and display at each stage of the filtering.
To give an example, I show how one could display the man page file.man found in the RPM source archive file-xxx.spm. The less command enhanced with the lesspipe.sh filter
yields the following output
Then the command
produces the output
The desired man page can finally be viewed with
The subcomponents of the argument to less were easily obtained by cut and paste using information contained in the previous lines of output. If you wanted to display the nroff sources instead, appending another colon at the end of the argument would have done the job:
If the man page was even compressed (e.g. as
Even extracting single files from an archive is possible, like with
The script is able to extract files up to a depth of 6 where applying a decompression algorithm counts as a separate level. In a few rare cases the file command does not recognize the correct format (especially with nroff). In such cases the filtering can be suppressed by a trailing colon on the file name.
The most recent additions to lesspipe.sh allow you to browse MSWord files (using the very fast antiword command) and look at contents of DOS formatted disks by accessing the proper device file.
To activate lesspipe.sh you have only to define the
Other file formats