How to Display Contents of a File in Linux
Why Viewing Files In Linux Is Important
Whether you are a regular user or an experienced system administrator, sooner or later, you will need to interact with files in Linux through the command line. For example, you might need to troubleshoot an issue by checking the log files, viewing your system’s details, or even customizing it by editing the configuration files.
Knowing how to display the contents of a file in Linux can make your life easier and save you time from constantly opening text editors. Many built-in functions make viewing files easy, fast, and tailored to your needs.
How to Display Contents of a File in Linux
Cat
The simplest way to view text files in Linux is the cat command. It displays the complete contents in the command line without using inputs to scroll through it.
Here is an example of using the cat command to view the Linux version by displaying the contents of the /proc/version file.

Head
Sometimes the information needed is in the first lines of a file. In that case, use the head command to view the first ten lines of a file in Linux. For example, users can display basic information about the CPU used by viewing the beginning of the /proc/cpuinfo file.

Like the tail command, use the -n flag with the head command to display the desired number of lines, starting from the beginning of a given file. For example, head -5 shows the first five lines of a given file.
Tail
While the cat command is helpful when dealing with a small file, it is not the best way to view large log files. The tail command allows viewing the last ten lines of a file by default instead of filling your terminal window with a wall of text, making it the perfect command to use if you want to check the last log entries.
Here is the output of the tail command.

Flags
A user can select how many lines the command should display by passing the -n flag (where n is an integer). For example, the tail -15 command will output the last 15 lines on a given file.
Another helpful flag used with the tail command is -f. It outputs the last ten lines of a file by default, but it also keeps displaying new entries as the file is updated. This function is beneficial when viewing the latest updates in log files to troubleshoot an issue. If you only want to use this functionality, you can use the tail -f command, and it will only display the entries that appeared in the file after running the command.
More
Another way to view file contents in Linux is the more command. It displays a file in the terminal, one page at a time. While using the more command, the Enter key scrolls through the file line by line, or the Space key scrolls one full screen at a time. Finally, you can close the file by pressing the Q key.
Here is an example of using more to display the contents of the cpuinfo file in Linux.

Less
While more is a handy command, it does come with a drawback. After closing a file, its contents stay written in the terminal window, filling it with text, forcing users to either clear the window or scroll back up to find something. It can also be slow, as it loads the entire file though displaying only one page at a time.
That’s where the less command comes in handy. It’s very similar to more, but with the benefit of not keeping all the text in the terminal window. The less command also comes with a built-in search function, allowing you to highlight the parts of the file for which you are looking. To search with less, press the forward slash key followed by the text you want to search.
The following is what the search function looks like if we search for /cpu in the cpuinfo file.

Tac
Another exciting way to display the contents of a file in Linux is in reverse order. To do so, use the tac command. It is similar to cat but reversed, reading and displaying the file starting from the last line. For example, here is what the output of tac looks like used to display the contents of the cpuinfo file.

For better readability, pipe the tac command into less to scroll through the file. Users accomplish piping using the desired command, the pipe character, and the other command. The syntax is as follows.
tac | less
Here is an example output from this command.

Grep
While not used for displaying the contents of a file, the grep command is handy for filtering the output of commands. For example, grep works for searching for specific text in a file.

In addition, pipe the output of other commands through grep, narrowing the search to what we are looking for in the file.
Here is an example of piping the output from the head command into the grep command.

Conclusion
While it might seem intimidating at first, learning how to display the contents of a file in Linux will make it significantly easier to navigate through any Linux distribution. These skills save time and make your job easier.
Do you already know how to display the contents of a file in Linux and just need hosting? Liquid Web has managed hosting options to help you get started with your next project. Contact our sales team to make your purchase.
Related Articles:
- How to Install Apache Tomcat on Linux (AlmaLinux)
- ChatGPT Integration — How to Create a Plugin for ChatGPT
- Stable Diffusion AI Image Generator (SDXL) — Using the Web UI
- How to Install VMware Tools on Ubuntu: Step-by-Step Guide
- How to Install WordPress on Linux (AlmaLinux)
- What is CentOS? Everything You Need to Know

About the Author: Dwight Kraft
Dwight is a 3rd year IT student that is passionate about computer hardware and pretty much everything electronics-related. In his free time, he builds PCs and plays guitar for fun.
Our Sales and Support teams are available 24 hours by phone or e-mail to assist.
Latest Articles
How to Install Apache Tomcat on Linux (AlmaLinux)
Read ArticleWhat is CentOS? Everything You Need to Know
Read ArticleWhat is CentOS? Everything You Need to Know
Read ArticleRedis as Cache: How It Works and Why You Should Use It
Read ArticleRefer-a-Friend Program for Website Hosting: Get $100 for Each Friend!
Read Article