Categories: TutorialsUbuntu

How to Display Data from a Text File in Linux

As a Linux user, you have to deal with text files all the time like source codes, log files, configuration files, and so on, to configure the text files and for this, we display data of text files. In this article, we will discuss different approaches to extracting data from text files and displaying it using Ubuntu 20.04 Linux Operating System.

Sponsored

You will know about the text file but in case you want to remember it, it contains raw data without any formatting, no audios, videos, or pictures just text data as its name suggests. They are used for storing information. Most text files have a “.txt” extension but other source files having extensions like “.cpp”, “.py” and some others are also included in text files.

In this Article, methods to display data of text files on the Ubuntu terminal screen will be discussed in detail. Follow the Article to know more about displaying data of text files.

Displaying Data from a Text File on Linux Terminal

Seven approaches to display data of Text files on Linux terminal are mentioned below:

  • Display Data from a Text File using cat command
  • Display Data from a Text File using nl command
  • Display Data from a Text File using less command
  • Display Data from a Text File using more command
  • Display Data from a Text File using head command
  • Display Data from a Text File using tail command
  • Display Data from a Text File using misc command

Below each method is discussed in detail.

Displaying Data from a Text File Using Cat Command

The cat stands for concatenate; it is preinstalled in new Ubuntu versions but if you are using an older version you need to install it. It is a commonly used command that reads all data from a file and outputs its content on the terminal screen. It allows us to generate, view, and combine files. When you use the cat command to display the contents of huge text files to the terminal, it will muck up your terminal and make navigation difficult.

Syntax:

$ cat [options] filename

Options will help in formatting the display content of the file.

Options Explanation
-A Equal to -vET
-b Display all non-empty output lines with numbering
-e Equal to -vE
-E Put $ at the end of each output line
-n Display all output lines with numbering
-s Repeated empty output lines are suppressed.
-t Equal to -vT.

The below-mentioned command will display all the content of the “linux.txt” file on screen:

$ cat linux.txt

Run the below-mentioned command to know more about the cat command:

$ man cat

Displaying Data from a Text File Using the nl Command

The nl command is preinstalled. It is similar to cat except the nl command is used for numbering lines that accepts input from a file or STDIN and copy each provided file to STDOUT, appending line numbers to the beginning of each line and displaying them on the terminal.

Syntax:

$ nl [options] filename
Options Explanation
-b Numbers the body lines
-i Increments the number on every line
-n Insert line number with respect to format
-v Changes the first line number of input
-s after each reasonable line number, adds the STRING

The below-mentioned command will display all the content of the “linux.txt” file on the screen using nl command:

$ nl linux.txt

Run the below-mentioned command to know more about the nl command:

$ man nl

Displaying Data from a Text File Using less Command

Less is a pre-installed command that allows you to look at a file’s contents a single page at a time. You can scroll through the text file by pressing the space key. Every page of the text file is indicated by two colons at the bottom of the terminal. You can exit by pressing “q”.

Syntax:

$ less [options] filename

It works great with bigger files, as it displays one page at a time so bigger files can easily be viewed by this command, below mentioned command will display all the content of the “linux.txt” file on-screen using less command:

$ less linux.txt

Sponsored

Run the below-mentioned command to know more about less command:

$ man less

Displaying Data from a Text File Using more Command

More is a pre-installed command that is similar to less except that it opens the text file, you can read page after page, and there will be no output visible on the screen when you exit. Your terminal will be spotless.

Syntax:

$ more [options] filename 

The below-mentioned command will display all the content of the “linux.txt” file on the screen using more commands:

$ more linux.txt

Run the below-mentioned command to know more about more command:

$ man more

Displaying Data from a Text File Using head Command

The Head command is another approach to see a text file; however, it differs slightly. By default, the head command displays the “top 10 lines” of a specified text file. By using different options of head command, you can change the way of displaying content. It comes pre-installed with all Linux distributions.

Syntax:

$ head [options] filename 

The below-mentioned command will display all the content of the “linux.txt” file on screen using the head command:

$ head linux.txt

Run the below-mentioned command to know more about the head command:

$ man head

Displaying Data from a Text File Using tail Command

The tail is a pre-installed command, the inverse of the head command. By default, the head command displays the “last 10 lines” of a specified text file. By using different options of head command, you can change the way of displaying content.

Syntax:

$ tail [options] filename 

The below-mentioned command will display all the content of the “linux.txt” file on the screen using the tail command:

$ tail linux.txt

Run the below-mentioned command to know more about the tail command:

$ man tail

Displaying Data from a Text File Using misc Command

If you don’t have either of the commands mentioned above, you can show the contexts of a file using a text editor like nano. However, rather than reading the contents, this is more like editing the file. It is preinstalled in new Ubuntu versions but if you are using an older version you need to install it.

Syntax:

$ nano filename 

Below mentioned command will display all the content of the “linux.txt” file on the screen using the nano command, press CTRL+s to save and CTRL+X to exit:

$ nano linux.txt

Conclusion

Text files are files that are used to store information. We need to configure text files daily, for this we want to display the content of text files. In this article, we discuss many ways of extracting the text from text files and displaying them on the terminal like displaying data from a text file using cat, nl, less, more, head, tail, and misc. go through the article thoroughly to know about each command in detail.

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications including CCNA RS, SCP, and ACE. As an IT engineer and technical author, he writes for various websites.

Ubuntu Server Admin

Recent Posts

Building RAG with enterprise open source AI infrastructure

One of the most critical gaps in traditional Large Language Models (LLMs) is that they…

16 hours ago

Life at Canonical: Victoria Antipova’s perspective as a new joiner in Product Marketing

Canonical is continuously hiring new talent. Being a remote- first company, Canonical’s new joiners receive…

2 days ago

What is patching automation?

What is patching automation? With increasing numbers of vulnerabilities, there is a growing risk of…

3 days ago

A beginner’s tutorial for your first Machine Learning project using Charmed Kubeflow

Wouldn’t it be wonderful to wake up one day with a desire to explore AI…

4 days ago

Ubuntu brings comprehensive support to Azure Cobalt 100 VMs

Ubuntu and Ubuntu Pro supports Microsoft’s Azure Cobalt 100 Virtual Machines (VMs), powered by their…

4 days ago

Ubuntu Weekly Newsletter Issue 870

Welcome to the Ubuntu Weekly Newsletter, Issue 870 for the week of December 8 –…

4 days ago