Software & AppsOperating SystemLinux

How To Open Files with Default Applications from Command Line

Ubuntu 12

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 xdg-open command.

Quick Answer

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.

Introduction to xdg-open

The 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.

Using 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:

xdg-open path/filename

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:

xdg-open Documents/example.pdf

This command will open the example.pdf file in your default PDF viewer.

Understanding File Associations

The 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

The 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:

xdg-open http://example.com

This command will open the specified URL in your default web browser.

Creating an Alias for xdg-open

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 .bashrc file:

alias open='xdg-open'

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.

Conclusion

The 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.

What is the difference between `xdg-open` and `gnome-open`?

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.

Can I use `xdg-open` to open files with specific applications?

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.

Leave a Comment

Your email address will not be published. Required fields are marked *