How to Display Contents of a Text File on Command Line Linux?

Displaying the contents of a text file on the Linux command line is useful to visualize the contents of the file without opening it. It is important to check the contents of log files, configuration files, script files, or any other text file type. Additionally, users can utilize the nano editor to edit or view the text file content.

This tutorial will demonstrate various methods to display the text file contents on the command line Linux.

Method 1: Using cat Command

The “cat” command is the simplest and most popular command to view a file in Linux and can be used with the filename as follows:

$ cat file.txt

The output displays the complete contents of the “file.txt” in the command line without using any inputs.

Method 2: Using less Command

The “less” command is similar to the “cat” command but visualizes the content of a file one page at a time. Let’s practice it on a filename “file.txt”:

$ less file.txt

The output shows the content of a specific text file in the command line.

Method 3: Using more Command

In Linux, the “more” command is comparable same to the “less” command, but it displays the content and terminates at the end of the particular file without extra spaces:

$ more file.txt

The output returns the content that is present in the file in the command line terminal.

Method 4: Using head Command

This command shows the first 10 lines of the particular file. However, you can modify the number of lines using the “-n” option.

To Get First Ten Lines:

$ head file.txt

The output returns the content present in the first ten lines of “file.txt” in the terminal.

To Get a Specific Number of Lines:

Users can utilize the “-n” option to present the specific number of lines from the top. For this, mention the “5” as “n” to print the top 5 lines in the terminal:

$ head -5 file.txt

The output shows the first 5 lines from the particular file in the terminal window.

Method 5: Using tail Command

Users can utilize the “tail” script to display the last 10 lines of the mentioned file. To do so, specify the filename as “file.txt” to visualize the bottom ten lines:

$ tail file.txt

The outcome of the above command represents the content that is present in the last ten lines of the “file.txt”.

Method 6: Using nano Editor

Users can utilize a text editor like “nano” to present the contents of a file and the editing services as well.

$ nano file.txt

The output displays the content of the “file.txt” file in the command line.

Conclusion

Linux offers the “cat”, “less”, “more”, “head”, and “tail” commands to display the contents of a text file on the Linux command line. Additionally, users can utilize the “nano” text editors to edit or view the content in a particular file. This guide briefly explained different methods to visualize the content in the specified text file.