- Unix Commands Reference
- Unix Commands - Home
less command in Linux with Examples
Name
less - read and display contents of text file one page per time
Synopsis
less filename
Options
Frequently used options
-g Highlights just the current match of any searched string. -i Case-insensitive searches. -m Shows more detailed prompt, including file position. -N Shows line numbers (useful for source code viewing). -S Disables line wrap ("chop long lines"). Long lines can be seen by side scrolling. -X Leave file contents on screen when less exits. -? Shows help. +F Follow mode for log.
Frequently used commands
Space bar Next Page d Next half Page b Previous Page u Previous half Page v Edit Content j or ↵ Enter Next Line k Previous Line Home Top of file End End of file F Follow Mode (for logs). Interrupt to abort. g or < First Line G or > Last Line ⟨n⟩+G Line ⟨n⟩ /⟨text⟩ Forward Search for ⟨text⟩. Text is interpreted as a regex. ?⟨text⟩ Backward Search like / n Next Search Match N Previous Search Match Esc+u Turn off Match Highlighting (see -g command line option) -+⟨c⟩ Toggle option ⟨c⟩, e.g., -i toggles option to match case in searches m+⟨c⟩ Set Mark ⟨c⟩ '+⟨c⟩ Go to Mark ⟨c⟩ = or Ctrl+G File information :n Next file :p Previous file h Help. This is presented with less, q to quit. q Quit
Description
less is a command that displays file contents or command output one page at a time in your terminal.
Less is a program similar to more, but it has many more features. 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. Less uses termcap (or terminfo on some systems), so it can run on a variety of terminals. There is even limited support for hardcopy terminals(On a hardcopy terminal, lines which should be printed at the top of the screen are prefixed with a caret.)
Mark Nudelman initially wrote less during 1983–85, in the need of a version of more able to do backward scrolling of the displayed text. The name came from the joke of doing "backwards more." Originally, less was developed for Unix, but it has been ported to a number of other operating systems, including MS-DOS, Microsoft Windows, OS/2, and OS-9, as well as Unix-like systems such as Linux.
Examples
less can be invoked with options to change its behaviour, for example, the number of lines to display on the screen. A few options vary depending on the operating system. While less is displaying the file, various commands can be used to navigate through the file. These commands are based on those used by both more and vi. It is also possible to search for character patterns in the file.
By default, less displays the contents of the file to the standard output (one screen at a time). If the file name argument is omitted, it displays the contents from standard input (usually the output of another command through a pipe). If the output is redirected to anything other than a terminal, for example a pipe to another command, less behaves like cat.
I’m using syslog output as input to less command in following examples.
$ cat /var/log/syslog|less Jan 24 09:16:36 rajat-Aspire-ES1-572 rsyslogd: [origin software="rsyslogd" swVersion="8.32.0" x-pid="919" x-info="http://www.rsyslog.com"] rsyslogd was HUPed Jan 24 09:17:01 rajat-Aspire-ES1-572 CRON[9970]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly) Jan 24 09:17:22 rajat-Aspire-ES1-572 anacron[927]: Job `cron.daily' terminated Jan 24 09:17:22 rajat-Aspire-ES1-572 anacron[927]: Normal exit (1 job run) Jan 24 09:25:01 rajat-Aspire-ES1-572 CRON[10117]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1) Jan 24 09:25:05 rajat-Aspire-ES1-572 gnome-software[9064]: no app for changed [email protected] Jan 24 09:25:05 rajat-Aspire-ES1-572 gnome-shell[2225]: Object .Gjs_AppIndicatorIconActor__1 (0x557a81b5c840), has been already finalized. Impossible to set any property to it. Jan 24 09:25:05 rajat-Aspire-ES1-572 org.gnome.Shell.desktop[2225]: == Stack trace for context 0x557a7ffc6330 == Jan 24 09:25:05 rajat-Aspire-ES1-572 org.gnome.Shell.desktop[2225]: #0 0x7ffd59d28e20 I resource:///org/gnome/gjs/modules/_legacy.js:83 (0x7fd2e03b5de0 @ 87) Jan 24 09:25:05 rajat-Aspire-ES1-572 org.gnome.Shell.desktop[2225]: #1 0x557a803549b0 i /usr/share/gnome-shell/extensions/[email protected]/indicatorStatusIcon.js:93 (0x7fd295dc2340 @ 58) Jan 24 09:25:05 rajat-Aspire-ES1-572 org.gnome.Shell.desktop[2225]: #2 0x7ffd59d29a00 I resource:///org/gnome/gjs/modules/_legacy.js:82 (0x7fd2e03b5de0 @ 71) Jan 24 09:25:05 rajat-Aspire-ES1-572 org.gnome.Shell.desktop[2225]: #3 0x7ffd59d29ac0 b self-hosted:916 (0x7fd2e03f12b8 @ 367) Jan 24 09:25:05 rajat-Aspire-ES1-572 org.gnome.Shell.desktop[2225]: #4 0x7ffd59d29bb0 b resource:///org/gnome/gjs/modules/signals.js:128 (0x7fd2e03d2230 @ 386) Jan 24 09:25:05 rajat-Aspire-ES1-572 org.gnome.Shell.desktop[2225]: #5 0x557a80354928 i /usr/share/gnome-shell/extensions/[email protected]/appIndicator.js:190 (0x7fd295dadf78 @ 22) Jan 24 09:25:05 rajat-Aspire-ES1-572 org.gnome.Shell.desktop[2225]: #6 0x7ffd59d2a800 I resource:///org/gnome/gjs/modules/_legacy.js:82 (0x7fd2e03b5de0 @ 71) :
To view the content of the a file using less command, use less [filename] syntax
$ less test2.txt a) a.k. shukla b) anat hari c) barun kumar d) jai sharma e) sumit singh f) jamun singh g) naman kumar h) mosin khan i) rahul kumar j) ajay narayan k) ram naresh l) ashu choudhary m) pandit ramphal n) gagan choubey o) utar purn p) kovid puram test2.txt (END)
If we want less command to show the line numbers,use -N option:
$ cat /var/log/syslog|less -N 1 Jan 24 09:16:36 rajat-Aspire-ES1-572 rsyslogd: [origin software="rsyslogd" swVersion="8.32.0" x-pid="919" x-info="http://www.rsyslog.com"] rs 1 yslogd was HUPed 2 Jan 24 09:17:01 rajat-Aspire-ES1-572 CRON[9970]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly) 3 Jan 24 09:17:22 rajat-Aspire-ES1-572 anacron[927]: Job `cron.daily' terminated 4 Jan 24 09:17:22 rajat-Aspire-ES1-572 anacron[927]: Normal exit (1 job run) 5 Jan 24 09:25:01 rajat-Aspire-ES1-572 CRON[10117]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1) 6 Jan 24 09:25:05 rajat-Aspire-ES1-572 gnome-software[9064]: no app for changed [email protected] 7 Jan 24 09:25:05 rajat-Aspire-ES1-572 gnome-shell[2225]: Object .Gjs_AppIndicatorIconActor__1 (0x557a81b5c840), has been already finalized. Imp 7 ossible to set any property to it. 8 Jan 24 09:25:05 rajat-Aspire-ES1-572 org.gnome.Shell.desktop[2225]: == Stack trace for context 0x557a7ffc6330 == 9 Jan 24 09:25:05 rajat-Aspire-ES1-572 org.gnome.Shell.desktop[2225]: #0 0x7ffd59d28e20 I resource:///org/gnome/gjs/modules/_legacy.js:83 (0x7 9 fd2e03b5de0 @ 87) 10 Jan 24 09:25:05 rajat-Aspire-ES1-572 org.gnome.Shell.desktop[2225]: #1 0x557a803549b0 i /usr/share/gnome-shell/extensions/ubuntu-appindicato 10 [email protected]/indicatorStatusIcon.js:93 (0x7fd295dc2340 @ 58) 11 Jan 24 09:25:05 rajat-Aspire-ES1-572 org.gnome.Shell.desktop[2225]: #2 0x7ffd59d29a00 I resource:///org/gnome/gjs/modules/_legacy.js:82 (0x7 11 fd2e03b5de0 @ 71) 12 Jan 24 09:25:05 rajat-Aspire-ES1-572 org.gnome.Shell.desktop[2225]: #3 0x7ffd59d29ac0 b self-hosted:916 (0x7fd2e03f12b8 @ 367) 13 Jan 24 09:25:05 rajat-Aspire-ES1-572 org.gnome.Shell.desktop[2225]: #4 0x7ffd59d29bb0 b resource:///org/gnome/gjs/modules/signals.js:128 (0x 13 7fd2e03d2230 @ 386)
When less command exits, the file contents will be cleared from the screen. To leave file contents on screen, use the -X option:
$ less -X test2.txt
When we are opening log files using less, +F option tells the less command to watch the file contents for changes.
$ cat /var/log/syslog|less +F Jan 24 16:40:00 rajat-Aspire-ES1-572 PackageKit: what-provides transaction /720_daaedcda from uid 1000 finished with success after 1773ms Jan 24 16:45:01 rajat-Aspire-ES1-572 CRON[15974]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1) Jan 24 16:50:16 rajat-Aspire-ES1-572 systemd[1]: Stopping PostgreSQL Cluster 10-main... Jan 24 16:50:16 rajat-Aspire-ES1-572 systemd[1]: Stopped PostgreSQL Cluster 10-main. Jan 24 16:50:25 rajat-Aspire-ES1-572 gnome-software[9064]: Only 0 apps for recent list, hiding Jan 24 16:50:26 rajat-Aspire-ES1-572 gnome-software[9064]: tried overwriting io.snapcraft.mockuuups-rnNB01JyovwveX9z2WauV2sG6fzPddmG key GnomeSoftware::FeatureTile-css from border-color: #000000;#012color: transparent;#012font-size: 30px;#012background: url('/home/rajat/.cache/gnome-software/cssresource/69480fd1ad760e5d03e4486c270fd542b8a20f75-Snap-Featured3x.jpg') no-repeat;#012background-position: center;#012background-size: contain; to border-color: #000000;#012color: transparent;#012font-size: 30px;#012background: url('https://dashboard.snapcraft.io/site_media/appmedia/2020/01/Snap-Featured3x.jpg') no-repeat;#012background-position: center;#012background-size: contain; Jan 24 16:50:26 rajat-Aspire-ES1-572 gnome-software[9064]: failed to call gs_plugin_refine_app on snap: Invalid string value converting to GVariant Jan 24 16:50:26 rajat-Aspire-ES1-572 gnome-software[9064]: failed to call gs_plugin_refine_app on snap: Invalid string value converting to GVariant Jan 24 16:50:27 rajat-Aspire-ES1-572 gnome-software[9064]: g_variant_new_variant: assertion 'value != NULL' failed Jan 24 16:50:27 rajat-Aspire-ES1-572 gnome-software[9064]: g_variant_new_variant: assertion 'value != NULL' failed Jan 24 16:50:27 rajat-Aspire-ES1-572 gnome-software[9064]: failed to call gs_plugin_refine_app on snap: Invalid string value converting to GVariant Jan 24 16:50:27 rajat-Aspire-ES1-572 systemd[1]: Reloading. Jan 24 16:50:27 rajat-Aspire-ES1-572 systemd[1]: Starting Message of the Day... Jan 24 16:50:27 rajat-Aspire-ES1-572 systemd[1]: Stopped PostgreSQL RDBMS. Jan 24 16:50:27 rajat-Aspire-ES1-572 systemd[1]: Reloading. Jan 24 16:50:28 rajat-Aspire-ES1-572 systemd[1]: Reloading. Jan 24 16:50:28 rajat-Aspire-ES1-572 PackageKit: what-provides transaction /721_bcaabedc from uid 1000 finished with success after 3060ms Jan 24 16:50:28 rajat-Aspire-ES1-572 50-motd-news[16171]: * Overheard at KubeCon: "microk8s.status just blew my mind". Jan 24 16:50:28 rajat-Aspire-ES1-572 50-motd-news[16171]: https://microk8s.io/docs/commands#microk8s.status Jan 24 16:50:28 rajat-Aspire-ES1-572 systemd[1]: Started Message of the Day. Jan 24 16:50:29 rajat-Aspire-ES1-572 PackageKit: resolve transaction /722_cdeadabb from uid 1000 finished with success after 611ms Waiting for data... (interrupt to abort)
The below command tells less to start at first occurrence of pattern "successful" in the file.
$ cat /var/log/syslog|less -p "successful" Jan 24 15:01:47 rajat-Aspire-ES1-572 NetworkManager[964]:[1579858307.3757] device (wlp2s0): Activation: (wifi) Stage 2 of 5 (Device Configure) successful. Connected to wireless network 'ESC SPECTRA-5G'. Jan 24 15:01:47 rajat-Aspire-ES1-572 NetworkManager[964]: [1579858307.3759] device (wlp2s0): state change: config -> ip-config (reason 'none', sys-iface-state: 'managed') Jan 24 15:01:47 rajat-Aspire-ES1-572 NetworkManager[964]: [1579858307.3763] dhcp4 (wlp2s0): activation: beginning transaction (timeout in 45 seconds) Jan 24 15:01:47 rajat-Aspire-ES1-572 NetworkManager[964]: [1579858307.3783] dhcp4 (wlp2s0): dhclient started with pid 14378 Jan 24 15:01:47 rajat-Aspire-ES1-572 avahi-daemon[905]: Joining mDNS multicast group on interface wlp2s0.IPv6 with address fe80::9b16:eab9:b8bb:9eca. Jan 24 15:01:47 rajat-Aspire-ES1-572 avahi-daemon[905]: New relevant interface wlp2s0.IPv6 for mDNS. Jan 24 15:01:47 rajat-Aspire-ES1-572 avahi-daemon[905]: Registering new address record for fe80::9b16:eab9:b8bb:9eca on wlp2s0.*. Jan 24 15:01:47 rajat-Aspire-ES1-572 kernel: [17998.070994] wlp2s0: Limiting TX power to 30 (30 - 0) dBm as advertised by 74:da:da:a0:0b:47 Jan 24 15:01:47 rajat-Aspire-ES1-572 wpa_supplicant[957]: wlp2s0: CTRL-EVENT-SIGNAL-CHANGE above=1 signal=-54 noise=9999 txrate=6000 Jan 24 15:01:47 rajat-Aspire-ES1-572 dhclient[14378]: DHCPREQUEST of 192.168.0.18 on wlp2s0 to 255.255.255.255 port 67 (xid=0x84768c9) Jan 24 15:01:50 rajat-Aspire-ES1-572 gnome-software[9064]: no app for changed [email protected] Jan 24 15:01:50 rajat-Aspire-ES1-572 gnome-software[9064]: no app for changed [email protected] Jan 24 15:01:50 rajat-Aspire-ES1-572 gvfsd-metadata[2554]: g_udev_device_has_property: assertion 'G_UDEV_IS_DEVICE (device)' failed Jan 24 15:01:50 rajat-Aspire-ES1-572 gvfsd-metadata[2554]: g_udev_device_has_property: assertion 'G_UDEV_IS_DEVICE (device)' failed Jan 24 15:01:50 rajat-Aspire-ES1-572 gnome-shell[2225]: [AppIndicatorSupport-DEBUG] Registering StatusNotifierItem :1.59/org/ayatana/NotificationItem/software_update_available Jan 24 15:01:50 rajat-Aspire-ES1-572 gnome-shell[2225]: [AppIndicatorSupport-DEBUG] Registering StatusNotifierItem :1.59/org/ayatana/NotificationItem/livepatch Jan 24 15:01:50 rajat-Aspire-ES1-572 dhclient[14378]: DHCPREQUEST of 192.168.0.18 on wlp2s0 to 255.255.255.255 port 67 (xid=0x84768c9) Jan 24 15:01:50 rajat-Aspire-ES1-572 dhclient[14378]: DHCPACK of 192.168.0.18 from 192.168.0.1 Jan 24 15:01:50 rajat-Aspire-ES1-572 NetworkManager[964]: [1579858310.7577] dhcp4 (wlp2s0): address 192.168.0.18 Jan 24 15:01:50 rajat-Aspire-ES1-572 NetworkManager[964]: [1579858310.7577] dhcp4 (wlp2s0): plen 24 (255.255.255.0) Jan 24 15:01:50 rajat-Aspire-ES1-572 NetworkManager[964]: [1579858310.7577] dhcp4 (wlp2s0): gateway 192.168.0.1 Jan 24 15:01:50 rajat-Aspire-ES1-572 NetworkManager[964]: [1579858310.7578] dhcp4 (wlp2s0): lease time 86400 Jan 24 15:01:50 rajat-Aspire-ES1-572 NetworkManager[964]: [1579858310.7578] dhcp4 (wlp2s0): hostname 'rajat-Aspire-ES1-572' Jan 24 15:01:50 rajat-Aspire-ES1-572 NetworkManager[964]: [1579858310.7578] dhcp4 (wlp2s0): nameserver '192.168.0.1' Jan 24 15:01:50 rajat-Aspire-ES1-572 NetworkManager[964]: [1579858310.7578] dhcp4 (wlp2s0): domain name 'Dlink' Jan 24 15:01:50 rajat-Aspire-ES1-572 NetworkManager[964]: [1579858310.7579] dhcp4 (wlp2s0): state changed unknown -> bound Jan 24 15:01:50 rajat-Aspire-ES1-572 avahi-daemon[905]: Joining mDNS multicast group on interface wlp2s0.IPv4 with address 192.168.0.18. Jan 24 15:01:50 rajat-Aspire-ES1-572 avahi-daemon[905]: New relevant interface wlp2s0.IPv4 for mDNS. Jan 24 15:01:50 rajat-Aspire-ES1-572 avahi-daemon[905]: Registering new address record for 192.168.0.18 on wlp2s0.IPv4. Jan 24 15:01:50 rajat-Aspire-ES1-572 whoopsie[1395]: [15:01:50] Cannot reach: https://daisy.ubuntu.com Jan 24 15:01:50 rajat-Aspire-ES1-572 NetworkManager[964]: [1579858310.7630] device (wlp2s0): state change: ip-config -> ip-check (reason 'none', sys-iface-state: 'managed') :
To Starts up the file from the given line number (less +number file_path):
$ less +4 test2.txt d) jai sharma e) sumit singh f) jamun singh g) naman kumar h) mosin khan i) rahul kumar j) ajay narayan k) ram naresh l) ashu choudhary m) pandit ramphal n) gagan choubey o) utar purn p) kovid puram ~ ~ ~ ~ test2.txt (END)
The -M option causes less to prompt even more verbosely than more:
$ cat /var/log/syslog|less -M Jan 24 09:25:05 rajat-Aspire-ES1-572 org.gnome.Shell.desktop[2225]: == Stack trace for context 0x557a7ffc6330 == Jan 24 09:25:05 rajat-Aspire-ES1-572 org.gnome.Shell.desktop[2225]: #0 0x7ffd59d28e20 I resource:///org/gnome/gjs/modules/_legacy.js:83 (0x7fd2e03b5de0 @ 87) Jan 24 09:25:05 rajat-Aspire-ES1-572 org.gnome.Shell.desktop[2225]: #1 0x557a803549b0 i /usr/share/gnome-shell/extensions/[email protected]/indicatorStatusIcon.js:93 (0x7fd295dc2340 @ 58) Jan 24 09:25:05 rajat-Aspire-ES1-572 org.gnome.Shell.desktop[2225]: #2 0x7ffd59d29a00 I resource:///org/gnome/gjs/modules/_legacy.js:82 (0x7fd2e03b5de0 @ 71) Jan 24 09:25:05 rajat-Aspire-ES1-572 org.gnome.Shell.desktop[2225]: #3 0x7ffd59d29ac0 b self-hosted:916 (0x7fd2e03f12b8 @ 367) Jan 24 09:25:05 rajat-Aspire-ES1-572 org.gnome.Shell.desktop[2225]: #4 0x7ffd59d29bb0 b resource:///org/gnome/gjs/modules/signals.js:128 (0x7fd2e03d2230 @ 386) Jan 24 09:25:05 rajat-Aspire-ES1-572 org.gnome.Shell.desktop[2225]: #5 0x557a80354928 i /usr/share/gnome-shell/extensions/[email protected]/appIndicator.js:190 (0x7fd295dadf78 @ 22) Jan 24 09:25:05 rajat-Aspire-ES1-572 org.gnome.Shell.desktop[2225]: #6 0x7ffd59d2a800 I resource:///org/gnome/gjs/modules/_legacy.js:82 (0x7fd2e03b5de0 @ 71) Jan 24 09:25:05 rajat-Aspire-ES1-572 org.gnome.Shell.desktop[2225]: #7 0x557a80354880 i /usr/share/gnome-shell/extensions/[email protected]/statusNotifierWatcher.js:219 (0x7fd295dad5e8 @ 225) Jan 24 09:25:05 rajat-Aspire-ES1-572 org.gnome.Shell.desktop[2225]: #8 0x7ffd59d2b3e0 I resource:///org/gnome/gjs/modules/_legacy.js:82 (0x7fd2e03b5de0 @ 71) Jan 24 09:25:05 rajat-Aspire-ES1-572 org.gnome.Shell.desktop[2225]: #9 0x557a80354808 i /usr/share/gnome-shell/extensions/[email protected]/extension.js:61 (0x7fd295d84780 @ 37) Jan 24 09:25:05 rajat-Aspire-ES1-572 org.gnome.Shell.desktop[2225]: #10 0x557a80354748 i resource:///org/gnome/shell/ui/extensionSystem.js:83 (0x7fd2e00592b8 @ 441) Jan 24 09:25:05 rajat-Aspire-ES1-572 org.gnome.Shell.desktop[2225]: #11 0x557a803546c8 i resource:///org/gnome/shell/ui/extensionSystem.js:354 (0x7fd2e0059d58 @ 13) Jan 24 09:25:05 rajat-Aspire-ES1-572 org.gnome.Shell.desktop[2225]: #12 0x7ffd59d2c0e0 b self-hosted:251 (0x7fd2e03c4ab0 @ 223) Jan 24 09:25:05 rajat-Aspire-ES1-572 org.gnome.Shell.desktop[2225]: #13 0x557a80354648 i resource:///org/gnome/shell/ui/extensionSystem.js:353 (0x7fd2e0059cd0 @ 64) Jan 24 09:25:05 rajat-Aspire-ES1-572 org.gnome.Shell.desktop[2225]: #14 0x557a803545c8 i resource:///org/gnome/shell/ui/extensionSystem.js:371 (0x7flines ~ 1-23
Easier file analysis using less:
$ file * | less command.txt: ASCII text data1.txt: ASCII text data2.txt: ASCII text Desktop: directory Documents: directory Downloads: directory examples.desktop: UTF-8 Unicode text Firefox_wallpaper.png: PNG image data, 1400 x 875, 8-bit/color RGBA, non-interlaced Functional-Foods-and-Nutraceuticals-20200814T140950Z-001.zip: Zip archive data, at least v2.0 to extract go: directory go1.14.3.linux-amd64.tar.gz: gzip compressed data htm: symbolic link to /var/www/html/ mongodb: directory mongodb-data: directory Music: directory mylsp.pem: PEM RSA private key MyTestApp: directory nltk_data: directory node_modules: directory package-lock.json: ASCII text pet-upload-6.4-28FEB2020: directory pgadmin.log: ASCII text Pictures: directory Public: directory Rajat: directory snap: directory stanford-corenlp-4.0.0: directory stanford-corenlp-4.1.0: directory stanford-corenlp-latest_up.zip: Zip archive data, at least v1.0 to extract stanford-corenlp-latest.zip: Zip archive data, at least v1.0 to extract stanza_corenlp: directory stanza_resources: directory Story.txt: UTF-8 Unicode text, with very long lines Templates: directory terms.txt: ASCII text test1.txt: ASCII text test2.txt: ASCII text :