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.
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.
Seven approaches to display data of Text files on Linux terminal are mentioned below:
Below each method is discussed in detail.
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 |
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 |
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 |
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 |
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 |
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 |
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 |
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.
One of the most critical gaps in traditional Large Language Models (LLMs) is that they…
Canonical is continuously hiring new talent. Being a remote- first company, Canonical’s new joiners receive…
What is patching automation? With increasing numbers of vulnerabilities, there is a growing risk of…
Wouldn’t it be wonderful to wake up one day with a desire to explore AI…
Ubuntu and Ubuntu Pro supports Microsoft’s Azure Cobalt 100 Virtual Machines (VMs), powered by their…
Welcome to the Ubuntu Weekly Newsletter, Issue 870 for the week of December 8 –…