What is the name of the Linux command that displays the contents of a file?

There are various ways to open a file in a Linux system. It is a fairly straight forward process to view the contents of a file, but if you are a new user, it may bother you. It is not as easy as opening a file in Notepad. From the Linux terminal, you must have some exposures to the Linux basic commands. There are some commands such as cat, ls, that are used to read files from the terminal.

In Linux, we can display various file formats such as text file, audio files, video, image, doc, pdf, or any other file contents.

What is the name of the Linux command that displays the contents of a file?

Following are some useful ways to open a file from the terminal:

This is the most popular and easy way to display the file content. It simply prints the file content to the terminal. It provides many options to make it more specific. To go in-depth with cat command, visit Linux cat.

To display the file content, execute the cat command as follows:

Let's create a file to understand how to open a file. Execute the below command:

The above command will create a text file 'Test.txt.' There are multiple ways to create a file, To learn, visit Linux Create File.

To display the file content of the above file, execute the command as follows:

cat Text.txt

Consider the below output:

What is the name of the Linux command that displays the contents of a file?

The cat command is a very useful utility to open a file, but the main issue with it that it displays the file content on the terminal. If we have a big file, then it is not an ideal way to open a file. In such a situation, the less command will be handy as it displays a page at a time.

2. Open File Using less Command

The less command allows us to view one page at a time.

To display the file content, execute the less command as follows:

The above command will display the content from the last line of the specified file. To go in-depth with the tail command, visit Linux Tail.

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

First:

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]

What is the name of the Linux command that displays the contents of a file?

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

What is the name of the Linux command that displays the contents of a file?

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

What is the name of the Linux command that displays the contents of a file?

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

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

What is the name of the Linux command that displays the contents of a file?

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