Beginner Linux users are often limited to the terminal. Viewing a file in the command line is a different idea to them, and it is strikingly different from viewing a file on a notepad in Windows. However, it is possible for the Linux terminal to display the contents of the file for you. This is a comprehensive guide about methods to display the contents of a file in Linux. 

Why Should You Learn File Viewing in Linux/Ubuntu?

No matter if you are a system admin or regular user, you will interact with files in Linux through the command line. Probably, you would have to check the log files for troubleshooting as an administrator. Sometimes, admins also customize the configuration files. A regular user may also have to view system details at any point. 

How to Display Content of Files in Linux/Ubuntu?

There are different commands in Linux to view the file content:

  • Using cat Command
  • Using nl Command
  • Using Head Command
  • Using Tail Command

Method 1: The Cat Command

Cat is the most famous and the most simple command to view file content in Linux. For this purpose, all you need to do is write cat then the filename, and you will see all the content of the selected file on your screen. However, it would not be a very good option when your file has 2000 lines of text. 

$ sudo cat alph.txt

Here, “alph.txt” represents the name of the targeted file. You can replace it with any valid file name of your choice. On successful execution of the cat command the content of the selected file will be shown as follows:

Method 2: The nl Command

The nl command works just like the cat command, but it adds a line number at the start of every line. 

$ sudo nl alph.txt

Method 3: The Head Command

Not every time you need to view the complete text of a file. Maybe we just need to view some of the starting parts of the text. The Head command is best for such situations. It only displays the first 10 lines of text.

$ sudo head alph.txt

Method 4: The Tail Command

Similarly, there are situations when the end portion of the file is important for you. It displays the file from the end.  

$ sudo tail alph.txt

Conclusion

The commands cat, head, tail, and nl can help you display the content of a file in Linux. All methods have their pros and cons. Users should pick the one that best suits their personal requirements. That may seem a little difficult at the start but becomes very easy after some hours of practice. This blog post has discussed several methods of displaying the content of a file in Linux/Ubuntu.