In the world of Linux, the command line is a powerful tool that can accomplish a wide range of tasks. One of these is opening files with their default applications. In this article, we will delve into how to achieve this using the
To open files with their default applications from the command line in Linux, you can use the
xdg-open command. Simply type
xdg-open followed by the path and filename of the file you want to open. This command will open the file in its associated application.
xdg-open command is a standard command available on most Linux distributions. It serves as a wrapper script that opens files or URLs in the user’s preferred application. If you’re working in a Linux environment, it’s likely you already have it installed.
xdg-open to Open Files
To open a file with its default application, you simply need to type
xdg-open followed by the path and filename of the file you want to open. Here’s the basic syntax:
In this command,
path/filename should be replaced with the actual path and filename of the file you want to open. For instance, if you have a PDF file named
example.pdf in your
Documents directory, the command would look like this:
This command will open the
example.pdf file in your default PDF viewer.
Understanding File Associations
xdg-open command opens files based on their file type associations. This means that an HTML file will open in a web browser, a PDF file will open in a document viewer, a text file will open in a text editor, and so on. These associations are typically set by your desktop environment or file manager.
Opening URLs with
xdg-open command isn’t limited to just files – it can also open URLs in your default web browser. The syntax is similar to opening files:
This command will open the specified URL in your default web browser.
Creating an Alias for
If you find yourself using the
xdg-open command frequently, you might find it useful to create an alias. This can be done by adding the following line to your
After saving the changes and restarting your terminal, you can use the
open command instead of
xdg-open. This can speed up your workflow and make the command easier to remember.
xdg-open command is a versatile tool that can open files and URLs in their default applications right from the command line. Whether you’re opening a document, viewing a webpage, or launching an application,
xdg-open can make your Linux experience smoother and more efficient.
xdg-open is a cross-desktop command that works on most Linux distributions, while
gnome-open is specific to GNOME desktop environment.
xdg-open is recommended for better compatibility.
Yes, you can use
xdg-open to open files with specific applications by specifying the application’s MIME type. For example, to open a PDF file with the application ‘evince’, you can use the following command:
xdg-open --mime-type application/pdf filename.pdf.