Home Linux Commands How to View Files in Linux Using cat, more, or less Commands

How to View Files in Linux Using cat, more, or less Commands

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
View File Contents in Linux
View File Contents in Linux

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 -A.

$ cat -A sample_file.txt
View Whole File Contents in Linux
View Whole File Contents in Linux

The symbol $ 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 -n.

$ cat -n sample_file.txt
View File Contents with Line Numbers
View File Contents with Line Numbers

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
View File Contents Using More Commands
View File Contents Using More Commands

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.

View Multiple Files Contents in Linux
View Multiple Files Contents in Linux

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
View File Contents Using Less Command
View File Contents Using Less Command

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
View File with Line Numbers
View File with Line Numbers

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.

Search String in Opened File
Search String in Opened File

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 
Open Multiple Files Contents in Linux
Open Multiple Files Contents in Linux

Pressing :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.

Ravi Saive
I am an Experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies. Founder of TecMint.com, LinuxShellTips.com, and Fossmint.com. Over 150+ million people visited my websites.

Each tutorial at UbuntuMint is created by a team of experienced writers so that it meets our high-quality standards.

Was this article helpful? Please add a comment to show your appreciation and support.

Got something to say? Join the discussion.

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published or shared. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.