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