×


Display Data from a Text File in Ubuntu 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. 

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.

Here at Ibmi Media, we shall look into extracting data from text files and displaying it using Ubuntu 20.04 Linux Operating System.


Different ways of Displaying Data from a Text File on Linux Terminal:

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


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


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


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

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

$ man less


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


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


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


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


[Need help in fixing Linux system issues ? We can help you. ]


Conclusion

This article covers the different 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. In fact, 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.