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
You can also use tail command:
less +F /var/log/message
tail -f /var/log/maillog
Start to display at a specific part in the file
# httpd -t
Output:
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.conf
Search and highlight particular text:# less "+/*VirtualHost" /etc/httpd/httpd.conf
Use 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_profile
Append following line:eval "$(lesspipe)"
Save file and load the changes:
. ~/.bash_profile
a) Display a PDF File:$ less file.pdf
b) Display a MS-Word File:$ less file.doc
c) Display listing of tar file (and other compressed files):$ less myfile.tar
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 myfile.tar.gz$ less KNOPPIX_V4.0.2CD-2005-09-23-EN.iso
e) Display .deb or rpm file information and file listing (useful to find out where files going to install):$ less foo.deb
less command supports following extensions:
$ less foo.rpm
- *.arj
- *.tar.bz2
- *.bz
- *.bz2
- *.deb, *.udeb
- *.doc
- *.gif, *.jpeg, *.jpg, *.pcd, *.png, *.tga, *.tiff, *.tif
- *.iso, *.raw, *.bin
- *.lha, *.lzh
- *.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.
🐧 8 comments so far... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
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.
Okai typo fixed. Thanks for the heads up
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. 😎
$ less foo.rmp
should be
$ less foo.rpm
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*
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.
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.
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
….