In Linux/Ubuntu systems, users and administrators often deal with text files in the form of configuration files, scripts, source codes, log files, etc. Text Files hold raw data, i.e., data without any formatting. The files having extensions .cpp, .c, .sh, and .py are also included in text files. Ubuntu 22.04 offers multiple commands to open a text file. This article will demonstrate different methods of opening a file from a Terminal in Ubuntu 22.04 LTS by
- Using the “cat” command
- Using “less” command
- Using the “nl” command
- Using the “more” command
- Using “head” command
- Using “tail” command
- Using “open” command
- Using the “xdg-open” command
In the following sections, each method is discussed in detail.
How to Open a Text File from a Terminal Using the “cat” command in Ubuntu 22.04?
The cat command is used to display, concatenate, and create files. A text file is opened via the cat command by the following syntax:
For example, a text file, “report4.txt” can be opened by replacing the
From the above image, it can be seen that instead of opening the text editor, the cat command displayed the entire content of the report4.txt file in Terminal.
How to Open a Text File from a Terminal Using the “less” command in Ubuntu 22.04?
The less command displays a text file one page at once. A text file is opened via the less command by the following syntax:
For example, a text file, “report1.txt” can be opened by replacing the
The remaining content of the file is on the next page which can be accessed by the down arrow button on the keyboard:
From the above image, it can be seen that instead of the text editor, the “less” command displayed the entire content of the report1.txt file in the Terminal.
How to Open a Text File from a Terminal Using the “nl” command in Ubuntu 22.04?
The nl command is used to display text files with numbered lines. A text file is opened via the nl command by the following syntax:
For example, a text file, “report4.txt” can be opened by replacing the
From the above image, it can be seen that the nl command displayed the entire content of the report4.txt file in the Terminal.
How to Open a Text File from a Terminal Using the “more” command in Ubuntu 22.04?
The more command displays a text file one screen at once. A text file is opened via the more command by the following syntax:
For example, a text file, “report6.txt” can be opened by replacing the
The remaining content of the file is on the next page which can be accessed by the down arrow button on the keyboard:
From the above image, it can be seen that more commands displayed the entire content of the report6.txt in the Terminal.
How to Open a Text File from a Terminal Using the “head” command in Ubuntu 22.04?
The head command is used to display the initial lines in a file. It prints the first ten lines of a file automatically. Additionally -n flag can be used to specify the required number of lines. A text file is opened via the head command by the following syntax:
For example, a text file, “report6.txt” can be opened by replacing the
From the above image, it can be seen that the head command displayed the first few lines of the report6.txt file in the Terminal.
How to Open a Text File from a Terminal Using the “tail” command in Ubuntu 22.04?
The tail command displays the last lines in a file. It prints the last ten lines of a file automatically. Additionally -n flag can be used to specify the required number of lines. A text file is opened via the tail command by the following syntax:
For example, a text file, “report6.txt” can be opened by replacing the
From the above image, it can be seen that the tail command displayed the last few lines of the report6.txt file in the Terminal.
How to Open a Text File from a Terminal Using the “open” command in Ubuntu 22.04?
The open command is used to open a file/directory/application/URL using the specified application name. The application name can be specified by using the -a flag followed by the open command and mentioning the application name. Having no specified application name, the file opens with the default application. It works as if a file is double-clicked in Ubuntu’s GUI.
For example, a text file, “report1.txt” can be opened by replacing the
The image indicates that the “open” command has launched report1.txt in the default application for opening text files, i.e., text editor.
How to Open a Text File from a Terminal Using the “xdg-open” command in Ubuntu 22.04?
The xdg-open command opens a file and URL in the preferred application of the user and the preferred browser of the user respectively. “xdg-open” supports files, URL, http, ftp, etc. If no application name is specified, then the file is opened using the default application for that file. A text file is opened via the tail command by the following syntax:
For example, a text file, “report2.txt” can be opened by replacing the
The image indicates that the “xdg-open” command has launched report2.txt in the default application for opening text files, i.e., text editor.
There are many more methods than the methods discussed above but in this article, the top eight methods are discussed.
Conclusion
Opening a text file from the Terminal in Ubuntu 22.04 is a straightforward process. A text file can be opened from a terminal by using cat, less, nl, more, head, tail, open, and xdg-open commands in Ubuntu 22.04. These files then either be viewed or edited in the command line or a text editor on Ubuntu’s UI. This article demonstrated different ways of opening a file from a Terminal in Ubuntu 22.04 LTS.
Discover more from Ubuntu-Server.com
Subscribe to get the latest posts sent to your email.