How do I Open a Text File in Linux Terminal?

In Linux, the text files are created by different text editors such as nano and vim text editors. The touch command line utility can also be used to create the file. Similarly, various commands can be used to open a text file in Linux.

In this tutorial, different command line utilities have been discussed to open the text files in Linux using the terminal by covering the below-mentioned topics:

Let’s start the blog!

Method 1: Using the cat Command

To view the content of the text file, the most convenient and recommended command utility is the “cat” command utility which can be used:

$ cat [file name]

To understand the usage of the above command, we will display the contents of the “MyFile.txt” using the cat command:

$ cat MyFile.txt

The contents of the file have been displayed.

Method 2: Using the more Command

The “more” command utility is similar to the less command utility. The only difference between both the commands is that more command leaves their output displayed on the screen without clearing it. Due to this reason, it is recommended to use the less command utility for opening files compared to the more command. The general syntax of using the more command in the Linux terminal is

$ more [file name]

For example, we will open the file “MyFile.txt” using the more command:

$ more MyFile.txt

The output has been displayed on the screen of Linux.

Method 3: Using the nl Command

The “nl” command utility can also be used to open the text file, and its usage syntax of the command is:

$ nl [filename]

The “nl” command is used to open the “MyFile.txt” using the command:

$ nl MyFile.txt

We can see in the above output, the lines have been displayed of the file with the line numbers.

Method 4: Using the xdg-open Command

The last command which can be used to open the text file in Linux is by using the “xdg-open” command utility. The general syntax of using the “xdg-open” command:

$ xdg-open [file name]

For example, we will understand its usage by opening the file:

$ xdg-open MyFile.txt

The file has been opened in Linux.

Method 5: Using the Text Editors

The text editors can also open and edit the text file. There are different text editors, such as “nano” and “vim”, but we will use the nano text editor as it comes pre-installed on most Linux distributions. The syntax of this command to open the text file is described below:

$ nano [file name]

For example, we will open our text file using the nano text editor:

$ nano MyFile.txt

Interestingly, you can view as well as edit the content of the file.

Note: To learn about the “vim‘ editor, click here to read our detailed article on “vim” commands.

Method 6: Using the less Command

We can also use the “less” command utility, which opens the text file by displaying its one page at a time and the general syntax of using the less command:

$ less [filename]

For example, we will open the “MyFile.txt” using the less command utility:

$ less MyFile.txt

After one page’s content, you must press the “Enter” key to view more content.

Moreover, you can exit the output by pressing the “q” key on the keyboard.

Alternate Methods

The methods listed in this section are also used to open up a text file with limited content. The “head” and “tail” command utilities are used to display only the first ten lines and the last ten lines, respectively.

tail Command

The “tail” command can also be used to serve the above-said purpose using the syntax provided below:

$ tail [file name]

To understand the usage of the tail command, we will open the “MyFile.txt” using the command:

$ tail MyFile.txt

The contents of the file have been displayed.

head Command

The “head” utility can be used following the below-stated syntax:

$ head [filename]

Following the above syntax of the head command, we will open the “MyFile.txt” using the command:

$ head MyFile.txt

The file has been opened in the terminal.

That’s all about this tutorial!

Conclusion

To open a text file in a Linux terminal, we can use the “cat”, “more”, “less”, “nl”, and “xdg-open” commands with different text editors. Moreover, the “head” and “tail” commands can also open a file with limited content. This method is super beneficial to get the content on the terminal. However, if you open it via text editors, you can edit the content as well. This blog has briefly explained all the possible methods to open a text file in Linux.