How to Open a File in Linux?

One of the main reasons to open a file in Linux is to read its content which can be useful when you want to see what is inside a file without making any changes. Another reason could be to modify the file’s content, which is useful when you need to make changes to a configuration file, script, or other text file types.

This article will show you different ways to open a file on Linux with the following outcomes: 

How to Open a File in Terminal?

In Linux, you can open a file by utilizing different commands in the terminal, which are explained below.

Using a cat Command

This is one of the simplest methods to open a file in Linux distribution by following the syntax mentioned below:

$ cat testfile.txt

You can also use to create a file using a cat command and later save its content as well by pressing “CTRL +D” using a following syntax shown:

$ cat > testfile.txt

For Large files it tries to fit its data on the terminal, but it is not possible then you can access that data by scrolling down as shown below:

Note: You can also use the cat command to open multiple files as well by following the below syntax:

$ cat file1.txt file2.txt

You can get more details about the cat command by reading this article.

Using less Command

The less command will open the file’s data separately, where you need to scroll down just like the above method to see the file’s content. To exit that file, you must press the ‘q’ key from the keyboard.

$ less testfile.txt

Note: You can also exit the screen when you reach the end of the file and go back to the terminal using the ‘-E’ flag as shown below:

$ less -E testfile.txt

Using more Command

The more command works in the same way as others, but it will show you the remaining document that is left in the form of percentage as shown below:

$ more testfile.txt

The above image is showing that only the 63% of the text is being displayed on the screen and the remain 37% can be seen by scrolling down. Another advantage is that it displays all the text in the same terminal. You don’t need to press any key as well to exit it as displayed below:

Using the nl Command

The number line command or nl is used to put a number at the start of each new paragraph as shown in the image below:

$ nl testfile.txt

Using nano Command

Linux comes with the built-in editor with the name of ‘nano’ that you can use not only to open files but also to modify them using the below command:

$ nano testfile.txt

You can also modify the content if you want to and later save the changes by pressing “CTRL +S” and close the file using “CTRL +X”.

Note: There is another great text editor that you can use with the name of “vim” that you can also use as an alternative to nano.

How to Open a File on a GUI?

There are multiple ways to open a file via GUI, which can be invoked through the terminal. 

Using gedit Command

In Linux, the “gedit” is a graphical text editor in Linux that enables you to create and edit text files you can use it by typing the below command:

$ gedit testfile.txt

Although you need to run the command through the terminal, the result can be seen in the GUI form as shown below:

Using xdg-open Command

The “xdg-open” command is used to open a file with the default application for its file type, so if you use      it to open a text file, it will be opened with the default text viewer, as shown below.

$ xdg-open testfile.txt

Directly Accessing the Directory

One of the easiest ways of opening any file in Linux is by “Double Click” on it or by right-clicking on the desired file selecting “Open with Text Editor”. 

Conclusion

To open a file using a terminal, you can use a command such as “cat,” “less,” “more,” “nl,” and “nano”. You can use the “gedit” and “xdg-open” commands to open a file using a GUI. Also, one additional method has been discussed, allowing you to open the file directly by accessing the directory. All these methods are practically demonstrated on Linux.