For viewing files, Linux has a number of commands. The cat command is a Linux command-line program that almost every Linux user knows. It allows you to view text files. However, the cat command is only useful when the output of the file content is short because when dealing with large files, you must scroll all the way up to view the file contents. When working with huge files, Linux less command may be a better solution.
In this tutorial, we will learn how to use less command in Linux. We also cover some of its useful options with examples.
- A Linux Operating System.
- Basic Command Line Knowledge.
- A good smile to learn.
What is less command in Linux
The less command is a Linux command-line utility that shows file content one page at a time, line by line. With less command, you can read large text files without crowding the terminal screen.
Less command has other features such as text search, log watch, line numbers, navigation control keys and commands, and more. Because the less command does not load the complete file at once but rather loads data in sections, it is quicker and easier to use.
How to use Less Command in Linux
The following is the basic syntax of less command.
less [Options] [File name]
- [Options] is the option available with the less command
- [File name] is the name of the file you want to display the command output.
The less command can be used without any options by simply specifying a filename of the command output to display the contents of a text file one screen at a time.
For example to view the file
/etc/ssh/ssh_config, type less command followed by the filename:
The output is shown one page at a time. However, can use the down arrow/up arrow key to scroll one line down or up. Use
Space bar to move down by one page and
b to move up by one page.
Until you quit the command, the command output remains in the terminal. If you want to exit the less command and return to the terminal, use the
q key from the keyboard.
The less command can also be used by taking the output of a command as the input for less command using the pipe. For example:
dmesg | less
Less Command in Linux Examples
Less is commonly used to read large text files including log files. It does have more options than linux more command.
The following are some of the less command's options explained with examples.
Display Line Numbers
-N option to display line numbers in the less command. The syntax for showing line numbers using the less command is as follows.
less -N [File name]
For example, to show line numbers in the file named
/etc/ssh/ssh_config, type the following in command-line.
less -N /etc/ssh/ssh_config
The command displays the file's output with line numbers in each line.
Display content on the terminal after exiting
After viewing the content of the less command, the output disappears. Use the -X option, if you wish to keep the content on the terminal after exiting.
For example, type the following in the command-line to leave the content of /var/log/dmesg file on the terminal. As the contents stay on the terminal, can scroll back to view the contents.
less -X /var/log/dmesg
Search for a string
You can use the less command to search for a string by using forward-slash (/) and the keyword. Once you hit Enter, less command will search forward for the matches. Press
n to repeat the search and
N in the reverse direction.
To search for the string
Port in the file
/etc/ssh/ssh_config, open the file using less command and then type:
Similarly, to pattern search backward use question-mark (?)
The highlighted text in the above image is the string we searched using the forward-slash or question-mark.
This search is case sensitive, to ignore case open the file using
Alternatively, you can search for a pattern using the
dmesg | less -p "kernel"
Redirect output using a pipe to less command
The output of other commands can be piped to the less command. This helps to scroll down each line at a time.
For example, the output of cat command is passed as input to less command using pipe:
cat /usr/share/adduser/adduser.conf | less
Using the above command displays the cat command output; however, it does not load the entire file at once.
Less command allows you to mark positions in a file and can go back to that position using the reference.
You may insert a mark by typing 'm' followed by a reference character. If you want to go back to that marked position, use a single apostrophe (') followed by the character typed previously.
For example, press
m when you need to set mark a place, type any letter as an identifier, here I pressed the letter 'f'.
To go to the mark press
'f from the prompt:
Monitor file in real-time
You may use the less command with the +F option to monitor the new lines being added to a file in real-time. The following is the syntax for monitoring the output in real-time using the less command.
less +F [File name]
It will display the file's final page before waiting for new data to be uploaded. In this mode, however, you cannot move up and down or back and forth as you normally would.
For example, to monitor dmesg log with less command, type the following command:
less +F /var/log/dmesg
This is very similar to the feature of tail command to monitor log by
tail -f command.
Viewing Multiple files
You can open multiple files using the less command. To display multiple files using the less command type the less command and the name of input files.
less [File name1] [File name2] …..
For example, to display multiple files text1.txt, text2.txt, text3.txt at the same time with the less command use the following syntax.
less file1.txt file2.txt file3.txt
To move to next file press
:n and to go back to previous file press
:p from the keyboard.
Less Command line Options
Some of the useful options of less command:
|This option print line number|
|This option leaves the content as it is in the terminal|
|This option Monitors file in real-time|
|This option displays the version of less|
|Ignore case when searching|
|Start search at the first occurrence of a certain pattern in the file|
|This option causes consecutive blank lines to be squeezed which means it removes blank lines and replaces them with one single blank line|
|This option disables all highlighting of strings discovered by search commands|
|This option replaces an existing file without asking you for confirmation|
|This option highlights the last search command's specific string|
|Display the summary of less commands|
Keyboard Navigation in Less Command
Here find some of the main keyboard shortcuts for easy navigation:
|Up arrow key, y or k||Scroll backward by one line|
|Down arrow key , or ENTER, e or j key||Scroll forward by one line|
|SPACE Bar or f||Move to the next page|
|B||Move to the previous page|
|Right Arrow Key||Scroll horizontally to the right|
|Left Arrow Key||Scroll the page Horizontally to the left|
|G||Move to the end of the file|
|g||Move to the start of the file|
|Q||Quit the less pager|
|ng||Move to the n th line of the output|
|Ctrl+F||Navigate forward one window|
|Ctrl+B||Navigate backward one window|
|Ctrl+G||Display filename, lines showing, bytes, and percentage statistics|
|j||Forward by one line|
|k||Backward by one line|
|:n||Move to the next file|
|:p||Move to the previous file|
|B||Move to the previous page|
|n||Repeat previous match search for next file|
|N||Repeat previous search in the reverse direction|
|/||Search pattern in forward direction|
|?||Text search in reverse|
|v||Allows to edit the file using the configured editor|
|h||Display summary of less commands while na|
Difference between less and more command in Linux
The fundamental difference between the more command and the less command is that the less command is faster since it does not load the complete file at once and allows you to go through it using the page up and page down keys. Less supports the following file formats: jar, war, zip, pdf, tar.gz, gif, png, tiff, tif, and rar.
Less command can be used to mark the location if you have a large file and it also allows you to monitor log files. You can also display line numbers using the less command. Both the more and less commands enable you to see numerous files at the same time. The less command comes with different keyboard navigation options to make it easier to view and monitor the log file conveniently. The less command can be used with other commands and redirect it to the less command.
The more command displays them as a single file divided by lines, but the less command allows you to move between them.
Cat is a basic Unix utility and an often-used command for concatenating the text file and printing them to the standard output. This is typically required when a file does not have read access for a certain user or group of users, although it is not restricted to the root user.
In this tutorial, we learned how to use the less command in Linux along with useful examples. For more information check the manual page or simply type
man less from the command line.