How to display file contents under Linux with cat, more and less?

Linux commands cat, more and less

Linux has a number of tools for displaying file contents, and 3 system-integrated commands are available in Linux: the cat, more and less commands. These command-line tools enable Linux users to analyze or browse existing files to retrieve specific information: useful in the configuration phase or when analyzing a problem, for example.

In concrete terms, these three commands will enable you to view files comfortably under Linux. Even though these controls have a lot in common, they work differently, so I'd like to present the 3 controls in a single article!

1. View files with the cat command

The cat command retrieves and displays the contents of a file directly in the Linux console. Here's an example with the file "tutobox.txt" in the current directory.

cat tutobox.txt
Display file contents under Linux with cat

With the cat command, you don't need to press Enter to scroll through the contents of the open file. You can use the scroll bar on the right-hand side of the Terminal window to scroll through the contents of the file.

To display the entire contents of a file, run the cat command with theoption -A.

cat -A tutobox.txt
Display the full contents of a file under Linux

The symbol $ appears at the end of each line to symbolize the end of the line.

If you want to number the entries on each line of the target file, use theoption -n. For example :

cat -n tutobox.txt
Display file contents with line numbers

This mode is very useful when you need to analyze an error and the error mentions the line number that is causing the problem.

L'command option -E also displays the end of the line with the symbol $. Finally, the -T option is also useful for highlighting tabs in the file with the "^I" symbol.

cat -T tutobox.txt

To conclude on the cat command, I'd say that it's recommended to use the cat command when dealing with small files that don't require too much scrolling. The reason? Using the cat command allows you to see the last line of the file directly, which makes it tedious to go back to the beginning of a file, especially when dealing with large files. In console mode too, as there's no scroll bar, it's not easy.

2. View files with the more command

The more command is a powerful tool for displaying the contents of a file while filtering the results. When the file is too large, this command-line tool lets you browse it one page at a time, depending on the size of your screen or window. With a small file, there's no real difference between cat and more, as the entire file will be displayed.

more tutobox.txt
Display the contents of a file using the more command

For the rest, I'll add a few lines to my "tutobox.txt" file...

As shown in the screenshot above, the more command displays the percentage (here 92%) of the file contents that can currently be viewed on screen. By pressing the Enter key on your keyboard, you'll be able to see the rest of the file's contents, page by page, so to speak.

The more command is also effective when a user wishes to view several files.

more tutobox.txt tutobox2.txt

This command will take us through the first file, then the second, depending on the order in the command.

Displaying the contents of multiple files under Linux

Suppose we want to view the lines of our file 10 by 10, rather than filling the screen with each one. This is written like this:

more -10 sample_file.txt

By default, the more command starts from the beginning to display the contents of the file. If you wish to start from line number 10, use this syntax:

more +20 tutobox.txt

3. Displaying files with the less command under Linux

The less command is an extension of the capabilities of the more command. It offers the same functionality as more, but with additional options.

For example, while the more command reads an entire input file before displaying it, the less command doesn't need an input file before being launched. It displays by page, too. As a result, the less command tends to execute faster on the same file than more and cat!

less tutobox.txt
Display the contents of a file using the Less command

By pressing the Enter key on your keyboard, you should be able to scroll to the end of the file, as with the "more" command.

We can also use the less command to display line numbers in a file:

less -N tutobox.txt

For a file already open, i.e. being read with the less command, it allows us to search for a specific string pattern when we type "/" and look for the desired expression. For example, if I type "/" and then "123", the less command highlights the lines containing this string.

Search for a string in an open file

If your file contains several empty lines, the -s option of the less command will eliminate them.

less -s tutobox.txt

Note the less command can also open multiple files, just like the more command.

The less command can also monitor file content in real time. So, if new content is added to the file, less will display it in the console as it is added. This function corresponds to the following option:

less +F tutobox.txt

After reading this article from the Computer Tutorials box, you'll be able to use the cat, more and less commands in Linux to display the contents of files!

Resources :

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *