Using a terminal on GNU/Linux often involves working with text files. This may not be necessarily about editing, only reading is enough to talk about this process as one of the most frequent. Corresponding editors are used as a tool for interacting with text files, but GNU/Linux offers simpler and more reliable solutions—“more” and “less” commands.
Both utilities act exclusively as viewers, including the output of other utilities. With their help, you can not accidentally make changes to the file. They have some additional features. They have common ideas and specific differences. This will be discussed in this article.
More Command in Linux: Syntax and Options
This command appeared earlier “less” and, as a result, has less functionality, but rather simple. The utility allows you to read a file in the terminal in one direction—down. This is the main feature.
The general syntax of more command is as follows:
more [options] file
More often, another option is used to work with redirecting the output stream:
previous command | more
Options are not required to be written, they only slightly change the method of viewing the text. Here are often used ones:
|-d||at the bottom of the screen will be a hint about managing the program|
|-uN||indicates the size of the screen to display the appropriate number of lines per page by default|
|+linenumber||starts viewing text from the specified line number|
|-s||compresses all empty lines in a row into one|
Basic control keys “more”:
|h or ?||shows a hint for using keys|
|SPACE||shows next lines of text (next page)|
|Nz||сhanges screen size (number of lines per page in the current session)|
|ENTER||shows next 1 line|
|d||shows next 11 lines|
|/||starts to search a text|
Less Command in Linux: Syntax and Keys
Less command has much more functionality. A distinctive feature of less is the ability to work with text in both directions (up and down), namely:
- scrolling backward and forward;
- searching backward and forward;
- quick jump to the first and last line.
The general less syntax is this:
less [options] file
Work with redirected output stream is standard.
The utility understands almost all of the same keys that are in “more”. Looking at controls in “less”, we can say that this is a read-only version of the vim editor.
Often used from control keys:
|j and k||scrolling the screen up and down respectively|
|gg (double press)||go to first line|
|G||go to last line|
|Ng||go to line number N|
|SPACE or Page UP||page forward|
|b or Page Down||page backward|
|F||go to the last line and wait for new data to be added (similar to tail -f)|
|/pattern||search for the word pattern forward in the text from the cursor|
|?pattern||search for the word pattern back in the text from the cursor|
|q||exit from the program|
|v||make changes to the text by the default editor|
Options for less are very rarely used. This is a rather specific topic; for more details, see the less reference guide.
Examples of using Less and More Commands
The more utility is almost never used now. Therefore, examples of use only for less will be given here.
View File with Less
The simplest and most frequently used operation, especially when redirecting output from another process:
cat -n /var/log/pacman.log | less
Real-time File Monitoring
As with the tail command, less has the ability to monitor changes to the file live:
less +F /var/log/pacman.log
To exit this mode and continue working in the file, press Ctrl + C.
View Multiple Files
Less can work with several files—just list them as utility parameters. To switch between them, use the combination of the colon keys and n / p (:n is the next file, and :p is the previous file):
less /var/log/pacman.log /var/log/Xorg.0.log
Please note that the name of the file and its number in the queue are written below.
No, the less utility itself cannot do this. It allows you to start the default editor and with it you can already make changes. This often happens when it becomes necessary to edit a file during normal viewing. But if you don’t have permission to edit the file, then it will be available all the same read-only.
For example, when starting the file /var/log/pacman.log as an ordinary user and trying to change it by pressing the v key, a warning will be written at the bottom of the terminal that the file can only be read:
Therefore, always pay attention to the file permissions. However, system security will not allow you to make changes to the file randomly.