Although it’s relatively simple to open any file to read or write content in it, the process might not look as easy as opening a file in a notepad if you are a beginner at Linux. So, using a few commands such as cat
, Is
, to read the files in the terminal, let’s find out how to open a file in Linux.
Linux supported file formats are text, audio, image, video, doc, pdf, or other files.
Also Read: 8 Linux Shutdown Commands with Examples
Table of Contents
Below are the seven important ways on how to open a file in Linux.
Function | Command |
Open a file using | cat |
Open a file using | more |
Open a file using | less |
Open a file using | nl |
Open a file using | gnome-open |
Open a file using | head |
Open a file using | tail |
How to open a file in Linux using cat command?
‘cat'
Command is the most popular when it comes to displaying the file contents in Linux. It’s used to print the content in the terminal with more options to provide functionality.
To execute the cat, let’s create a file to display contents.
cat <file name>
The below command will create a text file.
cat <demo.txt>
This is a demo file.
To display the file contents, consider the below command:
cat Demo.txt
techstricks@techstricks-inspiron-3542:~ Demo.txt
This is a demo file
techstricks@techstricks-inspiron-3542:~
How to open a file in Linux using more command?
The more
command in Linux is also a feasible way of showing the file content. The more
command cuts the content as per the width of the terminal.
You can use the below keys to scroll through the output:
Enter to scroll down through the page ‘Line by Line’.
Space to turn on towards the ‘next page’.
Press b to go backwards
‘/key:’ to search through the strings
Execute the following to display the file contents using more
command.
more Demo.txt
As per the width of the terminal window, the above command will display file contents.
techstricks@techstricks-inspiron-3542:~ more Demo.txt
This is a demo file
techstricks@techstricks-inspiron-3542:~
How to open a file in Linux using less command?
The less command is used to view the file contents one page at a time.
less Demo.txt
The below output will display one page at a time.
This is a demo file
Demo.txt (END)
Press ‘q‘ to return to the terminal.
How to open a file in Linux using nl command?
You can use the nl
command if you want to display the file content in line format with assigned numbers. Whereas the ‘nl'
command is similar to the.’cat'
command, the difference you can encounter is the prepended numbers while the output is displayed.
Follow the below command to display the associated output.
nl Demo.txt
The file contents will get displayed using the line number.
techstricks@techstricks-inspiron-3542:~ nl Demo.txt
This is a demo file
techstricks@techstricks-inspiron-3542:~
How to open a file in Linux using gnome-open command?
You can use the gnome-open command if you have the GNOME-based Linux system environment.
If you haven’t installed the gnome utility on Linux, follow the below steps.
sudo apt install libgnome2-bin
Execute the above command & enter your system administration password when asked to install the gnome utility.
Now that we have the gnome utility, we can execute the following command to display the file contents.
gnome-open Test.txt
The terminal will display the output in the default file system.
This is a text file
How to open a file in Linux using head command?
By using the head command, we can display the file content but in slightly a different way. The content is displayed in the first part of the file using the standard default input. It displays the file contents from the head (the 1st line) and the rest of the ten lines occurs from above.
You can execute the below command to open a file using the head command.
head <file name>
Content of the specified file is displayed here.
How to open a file in Linux using tail command?
The tail
command is quite similar to the head
command. The only difference is that the tail command is inverted then head. This means the contents will be read from the last line i.e tail. it also displays the output by ten lines.
Execute the below code to open the file using the tail command.
tail <file name>
Contents will be displayed here.