About nixCraft

Less is more: the hidden treasure of less command

Posted by Vivek Gite [Last updated: November 1, 2006]

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:

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.

Want to stay up to date with the latest Linux tips, news and announcements? Subscribe to our free e-mail newsletter or RSS feed to get all updates. You can Email this page to a friend.

You may also be interested in other helpful articles:

Discussion on This Article:

  1. Anonymous Says:

    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
    ….

  2. LinuxTitli Says:

    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.

  3. goku Says:

    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.

  4. Anonymous Says:

    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*

  5. Anonymous Says:

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

  6. Anonymous Says:

    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-)

  7. LinuxTitli Says:

    Okai typo fixed. Thanks for the heads up

  8. jp Says:

    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.

Leave a Reply

We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Copyright © 2004-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.