Most Linux users perform their routine activities via the terminal application. You can also do the same using the Graphical user interface in the Linux system.
We will explain in this guide how to open a file in a Linux system using the terminal as well as using the GUI.
Open file in Linux using Terminal
The following commands help you to open a file in the Linux system:
- Cat command
- Less command
- More command
- Nl command
- Head command
- Tail command
Let us discuss the working of each command one by one:
Cat is a simplest command to view the file content. When we use the cat command along with the file name. The cat command is used to view the file content on the terminal window.
$ cat testfile.txt
When we need to view the large file content. It is not considered useful in this case.
Less command shows the file content on the terminal. Using the less command, you can only view one page at a time. To view more content, you will scroll the output.
$ less testfile.txt
Press ‘q’ to exit from the less command. It will move you back to the terminal prompt.
More command displays the file content like the less command. The main difference between the less and more command is that the less command adjusts the height and width of the terminal windows but, the ‘more’ command cuts the file data according to the terminal width.
$ more testfile.txt
Head command is also used to open or view file content. Using the Linux head command, you can view or display the content from the start of the file. It shows the first 10 number of lines by default on the terminal. You can change this default behavior by using different options of head command. But, it displays the data from the beginning of the file.
$ head testfile.txt
The tail command prints the last number of lines from a file. When we use the tail command with the file name without any option, it retrieves the by default last 10 lines of that file. You can explore more tail command options by visiting the man page of the tail command.
$ tail testfile.txt
The nl command behaves like the cat command displays the file content on the terminal screen. The major difference between the cat and nl command is, the nl command also prepends the line number with each line on the terminal window.
$ nl testfile.txt
Open file in Linux using Gnome graphical environment
To open a file using the graphical interface, right-click on the selected file and choose the tool in which you want to open a file.
We have seen how we can open a file in Linux via a terminal. We have learned different commands through which you can easily view the file content using the command line. We also elaborated on how to open a file using GUI in Linux distribution.