Linux Commands

Display Contents Of A Text File On Command Line Linux

In Linux, we constantly work with text files such as configuration files, source codes, web pages, and many others. Hence, it is essential to have a quick view of the contents of a text file in the command line before editing the file.

This quick guide aims to show you various approaches you can use to list the contents of a text file in the terminal.


What is a text file?

The chances are high that you are familiar with a text file. However, to recap, a text file is a digital file that contains raw text; this means the file should not contain any formatting such as bold, italics, underline, or such. In addition, text files do not have any form of media such as pictures, videos, or audio.

By default, text files use the.txt extension. However, they take other forms such as source code in programming languages such as C (.c), C++ (.cpp, .h), Python (.py), and many more. Moreover, they do not necessarily have to end with an extension. For example, a configuration file such as /etc/vim/vimrc does not have any extension.

NOTE: We also call text files ASCII text files.

To view the file type in Linux, use the file command:

file /var/log/kern.log
/var/log/kern.log: ASCII text

# 1 – Cat

Cat is a popular and straightforward command for listing the contents of a file to the terminal.

To use the cat command, pass the name of the file to the cat command as:

cat [filename]

You can pass the absolute path to the file, as shown in the example above.

Cat is simple yet powerful when used with other options. To learn how to use the cat command, read -> how to use the cat command.

When using the cat command to dump the contents of a large text file to the terminal, it will mess up your terminal, making it very hard to navigate.

To resolve such as issue, we use the less command.

# 2 – Less

If you have ever read a manual page for any Linux command, then you have interacted with less.

Less allows you to view the contents of a file one page at a time. Using the space key, you can scroll through the text file. Two colons at the bottom of the screen indicate each page of the text file.

For example, a large file such as /var/log/kern.log would not work out great with a cat. To use less, we do:

less /var/log/kern.log

Once you reach the end of the file, you can scroll up and down using the UP and DOWN arrow keys.

To quit the less command, press Q. Less exits its session without messing up the terminal.

Combining less with a few options gives you control and functionality; for more, please read ->How to use less command with examples.

# 3 – More

Another command you can use to show the contents of a text file is the more command. More is very similar to the less command; however, it dumps the file’s contents to the terminal and exits at the end of the file.

Here is an example: (Same command as the other one?)

less /var/log/kern.log

# 4 –Head and Tail

Yes, there are such commands. The head and tail commands are very similar and used to show the first and last ten lines of a file, respectively.

However, you can modify how many first and last lines the head and tail command prints using the -n option.
For example, to show the first 15 lines of the kern.log file:

head -n 15 /var/log/kern.log

Similarly, to show the last 15 lines of the kern.log file:

tail -n 15 /var/log/kern.log

# 5 – Misc

If—for some reason—you do not have either of the commands discussed above, you can use a text editor such as nano to show the contexts of a file. However, this is more like editing the file than viewing the contents.

Here is the command for that:

nano /var/log/kern.log
# Not sure why you want to edit a log file


Now you know how to display the contents of a file on the Linux Command line. Thank you for reading.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list