Chapter 5. Redirection, Pipe and Shell Script

Less (Display Content with Pager)

Less (Display Content with Pager)
Tag:

The less command is functioning as a pager, which is used to read the contents of a text file one page(one screen) at a time.

Open a file with the less command

You can easily open the file with the less command.

Command Line - INPUT
less file_a

less command with pipe

less command is useful when the output of a command contains a large number of lines. For example, when you use the ls command with the -l option for a directory with multiple sub-directories and files, using the less command is helpful. The following is an example demonstrating a check of the list of contents of the /etc directory with its details.

Command Line - INPUT
ls -l /etc | less

You can see the result like the one below with : at the end. In this mode, you can scroll pages with keyboard actions.

Command Line - RESPONSE
total 796
drwxr-xr-x 3 root root       4096 Jul 16  2020 NetworkManager
drwxr-xr-x 2 root root       4096 Jul 16  2020 PackageKit
drwxr-xr-x 4 root root       4096 Jul 16  2020 X11
drwxr-xr-x 4 root root       4096 Jul 16  2020 acpi
-rw-r--r-- 1 root root       3028 Jul 16  2020 adduser.conf
drwxr-xr-x 2 root root       4096 Jul 16  2020 alternatives
drwxr-xr-x 3 root root       4096 Jul 16  2020 apparmor
drwxr-xr-x 7 root root       4096 Jul 16  2020 apparmor.d
drwxr-xr-x 3 root root       4096 Jul 16  2020 apport
drwxr-xr-x 7 root root       4096 Jul 16  2020 apt
-rw-r----- 1 root daemon      144 Nov 12  2018 at.deny
-rw-r--r-- 1 root root       2319 Feb 25  2020 bash.bashrc
:

Keyboard navigation

When you display file contents with the less command, you can use your keyboard to move around the document. Here are some frequently used keys.

  • and arrow keys: move one line
  • and arrow keys: scroll horizontally
  • space key: go to next page
  • b key: go to previous page
  • g key: jump to first line
  • G key: jump last line
  • q key: quit

IdeaNote: more command

There is another pager command called more. As the functionality of the more command is limited, using the less command for a pager gives a better user experience.

The less command is functioning as a pager, which is used to read the contents of a text file one page(one screen) at a time.

Open a file with the less command

You can easily open the file with the less command.

Command Line - INPUT
less file_a

less command with pipe

less command is useful when the output of a command contains a large number of lines. For example, when you use the ls command with the -l option for a directory with multiple sub-directories and files, using the less command is helpful. The following is an example demonstrating a check of the list of contents of the /etc directory with its details.

Command Line - INPUT
ls -l /etc | less

You can see the result like the one below with : at the end. In this mode, you can scroll pages with keyboard actions.

Command Line - RESPONSE
total 796
drwxr-xr-x 3 root root       4096 Jul 16  2020 NetworkManager
drwxr-xr-x 2 root root       4096 Jul 16  2020 PackageKit
drwxr-xr-x 4 root root       4096 Jul 16  2020 X11
drwxr-xr-x 4 root root       4096 Jul 16  2020 acpi
-rw-r--r-- 1 root root       3028 Jul 16  2020 adduser.conf
drwxr-xr-x 2 root root       4096 Jul 16  2020 alternatives
drwxr-xr-x 3 root root       4096 Jul 16  2020 apparmor
drwxr-xr-x 7 root root       4096 Jul 16  2020 apparmor.d
drwxr-xr-x 3 root root       4096 Jul 16  2020 apport
drwxr-xr-x 7 root root       4096 Jul 16  2020 apt
-rw-r----- 1 root daemon      144 Nov 12  2018 at.deny
-rw-r--r-- 1 root root       2319 Feb 25  2020 bash.bashrc
:

Keyboard navigation

When you display file contents with the less command, you can use your keyboard to move around the document. Here are some frequently used keys.

  • and arrow keys: move one line
  • and arrow keys: scroll horizontally
  • space key: go to next page
  • b key: go to previous page
  • g key: jump to first line
  • G key: jump last line
  • q key: quit

IdeaNote: more command

There is another pager command called more. As the functionality of the more command is limited, using the less command for a pager gives a better user experience.

Tag: