How to Open HTML File From a Terminal?

Html file is a combination of symbols and text which displays the content of the web page. These files have .html extensions. In Linux, various methods exist to open Html files using the terminal or a graphical user interface. In this post, you will learn how to open Html files using the terminal in Linux. The content of the post is:

Let’s get started

Method 1: Open HTML File in Browser

In Linux, you can open any Html file in your browser. There are two ways available for opening any Html file through the terminal. Let’s discuss these two functions one by one.

Using open Command

The “open” is a built-in utility to open any files or directories in Linux. The syntax to open any file using the “open” command is given below:

Syntax:

$ open [filename]

Write the “open” keyword and then type your Html file name.

Let’s move and check how it works. We have an index.html file in our home directory, which can be seen below:

$ ls

Let’s open it in the browser using the “open command”.

To open any Html file using the “open” command is obtained as follows:

$ open index.html

After executing the above command, it will automatically open the Html in your browser:

The index.html file is opened in the Firefox browser.

Using a Browser

You can open any Html file using a browser as well, and for that, you need to follow the below-mentioned syntax:

$ [browser name] [File Name]

Type any browser name and then type your Html file name.

To open any Html file using the browser name, execute the following command in the terminal:

$ chromium index.html

Once the above command is executed, the file will be opened in the browser. In our scenario, we have opened it in the chromium browser as shown in the below image:

The index.html file has been opened in the chromium browser.

Note: You can also use the bash script to open Html files in the browser. Just type in the bash script file and save it:

Once you save it, run the bash script file in the terminal:

$ ./b-script.sh

The execution of the above command will give you the same output.

Let’s move towards method 2 to open Html files.

Method 2: Open HTML File Using lynx

Another method that can be used to open an Html file is using the “lynx”. “lynx” is a terminal-based web browser that prints the output of the file as plain text in the terminal. In simple words, it’s a non-graphical web browser that shows the web pages on the terminal. It is not pre-installed in Linux distributions; user can install it manually by executing the below command:

For Ubuntu/Debian:

$ sudo apt install lynx

For Fedora/CentOS/RHEL:

$ sudo dnf install lynx

The syntax for using the “lynx” is shown below:

Syntax:

$ lynx [option] [file Name]

Type the “lynx” keyword, “options” for different modes and then file name.

To open an HTML file using “lynx”, use the “dump” option. “dump” option represents the standard output (stdout). Run the following command to see the results:

$ lynx -dump index.html

The content of the Html has been displayed as plain text.

Note: lynx will only print the Html content of the file. However, if you put any javascript content in an Html file, the javascript content will not be printed.

Using lynx, users can also open the Html file in its editor. To do so, use the lynx without option:

$ lynx index.html

Once you type the command, press Enter:

An Html file is opened in the lynx text editor.

These are the possible methods to open an Html file using the terminal.

Bonus Tip: How to Edit the Content of the HTML File?

There are different commands available to open and edit the files in the text editor, such as pico, nano, vi, and much more. In our case, we are using the “nano” editor to open the Html file. The syntax for the nano editor command is shown below:

Syntax:

$ nano [filename]

Write the “nano” keyword and then type the file name.

To open any Html file using the “nano” command is obtained as follows:

$ nano index.html

Once you execute the above command, it will open the Html file in the nano text editor from where you can edit the content of that HTML file.

Conclusion

To open any HTML file using the terminal, there are three methods. The first method is to open an HTML file in the browser using the “open” command or use any browser name along with the file name. The second method is to open an HTML file in text editors such as nano, while the third is to open an HTML file using lynx. This post has demonstrated all the possible ways to open an HTML file through a terminal in Linux.