Linux has numerous file viewing tools at its disposal and these tools give Linux users the flexibility of scanning or going through already existing files with the objective of fact-checking or simply retrieving specific information. Three in-built file viewing commands are availed in Linux, namely, cat, more, and less commands.
These three commands will comfortably let you view targeted files under the Linux operating system environment. Despite this shared commonality between the three commands, the functional originality defined for each of them is different.
Viewing Files with cat Command
The cat command concatenates provided input file and prints the content of that file as standard output.
$ cat sample_file.txt
With the cat command, you don’t need to press [Enter] to scroll through the open file. You can use the scroll on the right-side of the command-line window as depicted in the above screen capture.
To show all the content of a file, execute the cat command with the command option
$ cat -A sample_file.txt
$ will appear as the last entry in the file to symbolize the end-of-file. Also, the same symbol appears after each line entry to symbolize the end-of-line.
If you want to number the line entries in the targeted file, use the command option
$ cat -n sample_file.txt
Using the command option
-E will also show end-of-line and end-of-file through the
$ symbol. Another useful command option to consider is
-T which is used when displaying tab characters.
$ cat -T file_name
Based on the above demonstration, it is recommended to use the cat command when dealing with small files that do not require too much scrolling. Reason? Using the cat command zooms to the last line of the file making it tiresome to get back to the top/beginning of a file, especially when dealing with large files.
Viewing Files with more Command
The more command is a file perusal filter tool. When the file is too large, this command-line tool will page through it one screenful at a time.
$ more sample_file.txt
As per the above screenshot, more command shows the percentage (87%) content of the file which is viewable implying that pressing [Enter] on your keyboard should reveal the rest of the file content.
The more command is also effective when a user wants to view multiple files.
$ more sample_file.txt new.txt
This command will take us through the first file in the order in which they were presented and then introduce the next file in the queue.
Let’s assume we want to view 7 file lines at a time, we will implement the command:
$ more -7 sample_file.txt
If the file is numbered and you want to specify the line number from which the more command should display the file, e.g line number 20, we will execute:
$ more +20 sample_file.txt
Viewing Files with less Command in Linux
The less command is an extension of the more command’s capability. It achieves the same functionality as more but with additional features which make it a superior option.
For instance, while more command reads an entire input file before executing its file-viewing functionality, less command does not need an entire file’s input before launching.
Therefore, less command tends to be faster in its execution.
$ less sample_file.txt
Pressing [Enter] on your keyboard should enable you to scroll to end-of-file.
We can also use less command to show line numbers on a file:
$ less -N sample file.txt
For an already open file, the less command lets us search for a specific string pattern when we key in
[/] and search for the intended phrase.
If your file has multiple blank lines, the
-s command option will squeeze them out.
$ less -s sample_file.txt
The less command can also open multiple files.
$ less sample_file.txt new.txt
:n should take you to the next file and
:p to the previous file.
The less command also supports real-time monitoring, especially for files that are frequently edited.
$ less +F sample_file.txt
Let us know of another unique cat, more, or less command tricks that might be useful.