How to display data from a text file in linux

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.

See also  How To Reset Lost Admin Password For Grafana

Syntax:

$ cat [options] filename

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

Options Explanation
-AEqual to -vET
-bDisplay all non-empty output lines with numbering
-eEqual to -vE
-EPut $ at the end of each output line
-nDisplay all output lines with numbering
-sRepeated empty output lines are suppressed.
-tEqual to -vT.

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

$ cat linux.txt

How to display data from a text file in linux 1

How to display data from a text file in linux 2

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

$ man cat

How to display data from a text file in linux 3

How to display data from a text file in linux 4

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
-bNumbers the body lines
-iIncrements the number on every line
-nInsert line number with respect to format
-vChanges the first line number of input
-safter 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

How to display data from a text file in linux 5

How to display data from a text file in linux 6

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

$ man nl

How to display data from a text file in linux 7

How to display data from a text file in linux 8

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

See also  How to Install Python on Ubuntu 22.04?

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

How to display data from a text file in linux 9

How to display data from a text file in linux 10

Sponsored

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

$ man less

How to display data from a text file in linux 11

How to display data from a text file in linux 12

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

How to display data from a text file in linux 13

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

$ man more

How to display data from a text file in linux 14

How to display data from a text file in linux 15

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

How to display data from a text file in linux 16

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

$ man head

How to display data from a text file in linux 17

How to display data from a text file in linux 18

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

How to display data from a text file in linux 19

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

$ man tail

How to display data from a text file in linux 20

How to display data from a text file in linux 21

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

How to display data from a text file in linux 22

How to display data from a text file in linux 23

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

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.


Discover more from Ubuntu-Server.com

Subscribe to get the latest posts sent to your email.

Comments

No comments yet. Why don’t you start the discussion?

    Leave a Reply