Linux less Command Tutorial

The less command is one of the commands which is used to display and read file contents into the terminal in Linux and Unix systems. less command makes it very easy to navigate, search in a file with a keyboard shortcut. less command first created for Unix systems where there is no GUI and every task was completed via command line. less is only used to view and navigate a file or document but can not edit and save it. vi, vim, nano, etc. are complete text editors that provide display, search, and advanced edit options. less is generally used to read text files and log files. less name comes from the lesser which means print less information from the given file to read easily.

The less command provides similar function lile the more command. But the more command has less features then the less command and allows only forward but by using the less command the pages and content can be moved forward and backward.

less Command Syntax

The less command has the following simple syntax.

less OPTION FILE
  • OPTION provides different parameters and options for less command.
  • FILE is the file which will be read and displayed. Alternative to file another command output can be redirected too.

less Options

Even the less command provides a lot of options one of the most popular and useful options are like below.

OPTIONDESCRIPTION
-? or –helpDisplay help about less
-f or –forceForce non-regular files to be opened
-I or –ignore-caseIgnore case
-n or –line-numbersSuppress, do not show line numbers

Read File with less

The less command mainly used to read text and log files. In the following example we will read the log file named /var/log/syslog .The path of the file we want to read is provided as parameter.

$ less /var/log/syslog
less Command

From the screenshot, we can see that the content is displayed on the screen interactively. You can use the navigation and search commands for less. Also, the file name currently viewed is displayed in the bottom-left corner. Also the file name currently viewed is placed bottom right corner.

READ  Linux du Command Tutorial with Examples

Alternatively if the file is located in the current working directory we can use th only the file name like below to read page by page.

less syslog

The current working directory located files can be listed with the ls command like below.

ls

Quit less Interactive Mode

When using the less command it will be automatically entered the interactive mode which will hide the bash shell. In order to go back to the bash shell and close or quit the less interactive mode, the q shortcut can be used as below.

q

Navigate with less

After the less interactive mode is opened the following commands can be used to search, navigate in the interactive mode easily. Even bookmarks can be created and navigated later.

Keyboard ShortcutAction
Down Arrow Key , Enter , e , jMove down one line
Up Arrow Key , y , kMove up one line
Space Bar , fMove down one page
bMove up one page
Right Arrow KeyScroll horizontally to the right
Left Arrow KeyScroll horizontally to the left
gGo to the first line
GGo to the last line
10gGo to line number 10
80gGo to line number 80
50p , 50%Go to 50 percentage of the document
90p , 90%Go to 90 percentage of the document
/linuxSearch the term linux to forward or downward
?linuxSearch the term linux to backward or upward
nIn search go/jump to next occurrence
NIn search go/jump to the previous occurrence
m“aSave current location as a bookmark to the letter a
‘“aReturn to bookmark specified with the letter a
hDisplay help information
qQuit or exit

Follow Given File and Print Changes To The Terminal

By default, less command will read the given file content and print to the current terminal in with an interactive mode. But some files may change during the usage of the less command or we may need to follow the given file for new content. Especially log files are used to put new content in every second for a busy application. The less command can be used to follow and print the new content when new content is added into a file. We will use the -X option in order to follow a file interactively.

$ less -X /var/log/syslog

Show Contents In The Bash Terminal

By default the less command opens the interactive terminal in order to show given file content. After exiting from the less interactive terminal the content will be cleared from screen. If you need to leave the file content on the bash shell you can use the -X option like below.

$ less -X /var/log/syslog

Show Line Numbers

By default the less command only shown the file content without adding any extra data or information. But in some cases adding line numbers for every line for the file content can be beneficial. You can shown line numbers by using the -N option like below.

$ less -N /var/log/syslog
less Command Line Numbers

Redirect Command Output to less

Up to now we have read the content from a file. The less command can be also used to read content from a command output. We will just redirect the command output by using the pipe | operator like below.

$ cat /var/log/syslog | less

Exit From less

When executed the less command opens an interactive console which hides the bash shell. Even you reach to the end of file the less do not ends and exits. The q key can be used to close and exit the less interactive console.

q

Use less Command with dmesg Command

One of the most popular usacase for the less command is paging and viewer the dmesg command output. The dmesg command prints the kernel log to the standard output. Byt using the less command this output can be easily viewer page by page. Also the page up and page down keys can be used to navigate forward and backward.

dmesg | less

Leave a Comment