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:
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:
To rectify this, we can create an alias for the xdg-open command. The following example demonstrates creating a temporary Bash alias:
Verify if the alias was created successfully:
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:
Opening an URL
If we attempt to open a URL, the expected behavior is to open the URL in the default web browser.
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!