Linux open Command

2

In this guide, we will demonstrate using the open command in Linux.

Prerequisites

To perform the steps demonstrated in this guide, you will need the following components:

  • A properly configured Linux system. For testing, consider using a Linux VM.
  • Basic understanding of the command-line interface.

The open command

In Linux, the open command is a CLI tool that attempts to open a specified file, directory, or URL using the default program.
Check out the following example:

open https://linuxhint.com/

linux open command Linux open Command Linux open Command
linux open command Linux open Command 1714145332 699 Linux open Commandlinux open command Linux open Command 1714145332 699 Linux open Command

Here, the open command will open the URL on the default web browser.

open vs xdg-open

Some Linux systems use xdg-open (part of the xdg-utils package) instead of the open command. In practice, they both behave the same:

xdg-open https://example.com

linux open command Linux open Command 1714145332 612 Linux open Commandlinux open command Linux open Command 1714145332 612 Linux open Command
linux open command Linux open Command 1714145332 955 Linux open Commandlinux open command Linux open Command 1714145332 955 Linux open Command

To rectify this, we can create an alias for the xdg-open command. The following example demonstrates creating a temporary Bash alias:

linux open command Linux open Command 1714145332 830 Linux open Commandlinux open command Linux open Command 1714145332 830 Linux open Command

Verify if the alias was created successfully:

linux open command Linux open Command 1714145332 37 Linux open Commandlinux open command Linux open Command 1714145332 37 Linux open Command

linux open command Linux open Command 1714145332 597 Linux open Commandlinux open command Linux open Command 1714145332 597 Linux open Command

Note that various command arguments of the open command won’t work with xdg-open. Some distros implement the open command as a symlink to xdg-open (Ubuntu, for example).

Using the open Command

Opening Text Files
To open a text file in the default text editor/viewer, run the following command:

linux open command Linux open Command 1714145332 416 Linux open Commandlinux open command Linux open Command 1714145332 416 Linux open Command
linux open command Linux open Command 1714145332 669 Linux open Commandlinux open command Linux open Command 1714145332 669 Linux open Command

Opening an URL
If we attempt to open a URL, the expected behavior is to open the URL in the default web browser.

open https://archlinux.org

linux open command Linux open Command 1714145333 368 Linux open Commandlinux open command Linux open Command 1714145333 368 Linux open Command
linux open command Linux open Command 1714145333 882 Linux open Commandlinux open command Linux open Command 1714145333 882 Linux open Command

Opening a File using a Specific App
If not specified, the open command will use the default app to open the specified file/URL. However, we can specify a different program to use when attempting to open the file.

To open with a different program, the command structure is as follows:

We can also specify what app to use using bundle identifier:

Note that it won’t work with xdg-open.

Opening a File in a New Program Instance
If the file-associated program is already running, then open will use the already-running instance. In some situations, however, we may want to open the file in a new program instance.

To open the file with a new program instance, use the “-n” flag:

Note that this method will also not work with xdg-open.

Final Thoughts

In this guide, we demonstrated using the open command on Linux. It takes a file, directory, or URL as an argument and launches the default program designated to handle it.

Interested in learning about other Linux commands? Check out the Linux commands sub-category.

Happy computing!

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More