Less is more: the hidden treasure of less command

by on March 10, 2006 · 8 comments· LAST UPDATED November 1, 2006

in , ,

less is a pager or a program similar to more, but which allows backward movement in the file as well as forward movement. Less has a massive number of options. Every time I work with less command, I find something new and amazing. less is capable of displaying not just a text file but it can display an ISO image, a PDF file or even a MS word doc file contains from shell prompt. Sometime it saves lots of my time:

Watch a log file
Watch maillog in real time, useful to troubleshoot mail server problem
less +F /var/log/maillog
less +F /var/log/message
You can also use tail command:

tail -f /var/log/maillog

Start to display at a specific part in the file
# httpd -tOutput:

Syntax error on line 53 of /etc/httpd/httpd.conf
....
..
....

Ok let us display an error at line number 53 (no need to scroll down all 52 lines)# less +53 /etc/httpd/httpd.confSearch and highlight particular text:# less "+/*VirtualHost" /etc/httpd/httpd.confUse less preprocessor to display following extensions files on screen
lessfile and lesspipe are programs that can be used to modify the way the contents of a file are displayed in less. What this means is that less can automatically open up tar files, uncompress gzipped files, and even display something reasonable for graphics files. Just put the following command in your login script (if you are using bash put in ~/.bash_profile):
$ vi ~/.bash_profileAppend following line:eval "$(lesspipe)"Save file and load the changes:

. ~/.bash_profile

a) Display a PDF File:$ less file.pdfb) Display a MS-Word File:$ less file.docc) Display listing of tar file (and other compressed files):$ less myfile.tar
$ less myfile.tar.gz
d) Display an ISO image file listing without mounting them (remember to mount an ISO file you need to have a root account permission):$ less KNOPPIX_V4.0.2CD-2005-09-23-EN.isoe) Display .deb or rpm file information and file listing (useful to find out where files going to install):$ less foo.deb
$ less foo.rpm
less command supports following extensions:

  • *.arj
  • *.tar.bz2
  • *.bz
  • *.bz2
  • *.deb, *.udeb
  • *.doc
  • *.gif, *.jpeg, *.jpg, *.pcd, *.png, *.tga, *.tiff, *.tif
  • *.iso, *.raw, *.bin
  • *.lha, *.lzh
  • *.pdf
  • *.rar, *.r[0-9][0-9]
  • *.rpm
  • *.tar.gz, *.tgz, *.tar.z, *.tar.dz
  • *.gz, *.z, *.dz
  • *.tar
  • *.jar, *.war, *.xpi, *.zip
  • *.zoo

Finally, lesskey command lets you change the meaning of any key or sequence of keys for less command. lesskey man page has detailed information on this topic. Read man page of less command for more info.

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 8 comments… read them below or add one }

1 Anonymous March 11, 2006 at 4:41 pm

Hello,

may I ask what wonderous version of less you are using?

When viewing .jpg or .iso file less asks me “may be a binary file. See it anyway?” and then displays binary gibberish. 8-(

$ less –Version
less 382+iso254
….

Reply

2 LinuxTitli March 11, 2006 at 5:38 pm

Hi there,

less –version
less 394

I hope you have added following line to your .bash_profile (shell startup file):
eval “$(lessfile)”

Hope this helps.

Reply

3 goku March 11, 2006 at 5:43 pm

I am using Gentoo and it worke after upgrading less version :) may be you need an updated version of less.

Nice find especially displaying a PDF/doc files over remote ssh session. Last time I had to configure qmail and all information was in a PDF file. Therefore, I had taken printout of over 30+ pages from windows box.

Reply

4 Anonymous March 11, 2006 at 5:48 pm

less SUSE-9.3-Prof-i386-CD1.iso

CD-ROM is in ISO 9660 format
System id: LINUX
Volume id: SU930.F01
Volume set id:
Publisher id: Novell, Inc.
Data preparer id: CD-Team, http://www.suse.de/feedback
Application id: SuSE-Linux-Professional-FTP-i386-9.3#0
Copyright File id:
Abstract File id:
Bibliographic File id:
Volume set size is: 1
Volume set sequence number is: 1
Logical block size is: 2048
Volume size is: 356178
El Torito VD version 1 found, boot catalog is in sector 265
Joliet with UCS level 3 found
Rock Ridge signatures version 1 found
Eltorito validation header:
Hid 1
Arch 0 (x86)
ID ‘Novell, Inc.’
Key 55 AA
Eltorito defaultboot header:
Bootid 88 (bootable)
Boot media 0 (No Emulation Boot)
Load segment 0
Sys type 0
Nsect 4
Bootoff 10A 266

/ARCHIVES.gz
/COPYING
/COPYING.de
/COPYRIGHT
/COPYRIGHT.de
/ChangeLog

*Sweet*

Reply

5 Anonymous March 11, 2006 at 6:36 pm

$ less foo.rmp
should be
$ less foo.rpm

Reply

6 Anonymous March 11, 2006 at 8:20 pm

Hello,

it seems those are features of the cutting edge less 394.

With less 382 I seem to have no “lessfile” nor “lesspipe”.
8-(

Those features would be nice to have.

Anyway, nice article. 8-)

Reply

7 LinuxTitli March 11, 2006 at 8:45 pm

Okai typo fixed. Thanks for the heads up

Reply

8 jp February 18, 2008 at 9:13 am

if i use “ls -l | less -C” option, the output it shows covers the full screen. But i want only the output of that command alone, which will be less than half page. What is the option to give with less? Thanks.

Reply

Leave a Comment

Previous post:

Next post: