File manipulation is a common task across all operating systems, including Linux/Ubuntu. Ubuntu is one of the most popularly used platforms that allows us the file manipulation securely and efficiently. File manipulation involves various functionalities, such as file creation, file opening, and so on. Among all these file manipulation tasks one of the most fundamental ones is file opening.
This write-up revolves around different file-opening methods in Linux/Ubuntu terminals.
How to Open a Particular File Using the Linux/Ubuntu Terminal?
File opening allows us to read the content of a particular file or edit it according to users’ needs. In Linux/Ubuntu, a file can be opened either by using GUI or CLI. This post will specifically discuss the below-listed file opening approaches in a Linux terminal:
- Approach 1: Using the cat command
- Approach 2: Using less command
- Approach 3: Using more command
- Approach 4: Using nl command
- Approach 5: Using open command
- Approach 6: Using head command
- Approach 7: Using the tail command
Let’s understand how each command functions.
Approach 1: Use Cat Command to Open a File
The cat command is the simplest and easiest way to open a file in a Linux terminal. It displays the file data to the Ubuntu/Linux terminal. To use this command, specify the “cat” keyword followed by the name of the file to be opened:
$ cat filename
Approach 2: Use less Command to Open a File
The less command can also be utilized to see the file content (one page at a time). The syntax of executing the less command for opening a particular file is listed below:
$ less filename
The less command displays the file content in a new window. Press the “q” key to exit back to the Ubuntu/Linux terminal:
Approach 3: Use more Command to Open a File in Linux/Ubuntu
The “more” command can also be utilized to display the contents of the selected file (one page at a time). Here is how you can use it in a Linux terminal:
$ more filename
Approach 4: Use nl Command to Open a File
Another useful way of accessing a file is using the nl command which shows the contents of a file alongside the line number. Here is how you can use it in a Linux terminal:
$ nl filename
Approach 5: Use open Command To Open a File in Linux/Ubuntu
The open command is another alternate method to display the file content. However, it shows the content in the default text editor. Here is a basic syntax for opening a particular file using the “open” command:
$ open filename
It can be seen that the file’s content is shown in the default text editor.
Approach 6: Use the head Command to Open a File in Linux/Ubuntu
The head command is different from the other commands which show the first section of the file, usually the initial ten lines, however, the number of lines to be shown can be customized. The syntax is as follows:
$ head filename
Consider a sample text file shown below which consists of 17 lines.
Executing the “head” command retrieves the following output:
It can be seen that the first ten lines of the file are displayed.
Approach 7: Use the tail Command to Open a Specific File
The tail command opposes the functionality of the head command i.e., it reads the file from the bottom (last line). It retrieves the final ten lines of the file, however, the number of lines to be retrieved can be customized. The syntax of the tail command is demonstrated in the below code snippet:
$ tail filename
Consider the same example as above. Now, execute the tail command to get the last ten lines of the file named “file1.txt”.
It can be noticed that the final ten lines of the selected file are shown.
There are numerous approaches to help us open a file in a Linux terminal. These commands include cat, less, more, nl, open, head, and tail. Among them, the cat command is the simplest and easiest way to open a file in a Linux/Ubuntu terminal. All these commands are discussed with practical demonstration.