Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening a file with the default viewer on Linux

Tags:

c

linux

I am working at an OS independent file manager (mostly Windows and Linux), and I am wondering if there is a Linux way to open a file in its default viewer. For example, open a html file with Firefox, an .avi with vlc, and so on. On Windows, there is a function ShellExecute() that does that, but my understanding is that on Linux it is not that simple, and each desktop environment has a specific way. I would appreciate any help with this.

like image 651
Radu Avatar asked Sep 18 '11 09:09

Radu


People also ask

How do you open a file with a program in terminal Linux?

If you want to open any file with a specific application, just enter: open -a "Application" /Path/to/file .

How do I change file associations in Linux?

Files (nautilus) >> select a file >> right click & Properties >> "Open With" tab >> select a program you fancy >> "Select as default". Done.

How do I open a file with contents in Linux?

Cat. The simplest way to view text files in Linux is the cat command. It displays the complete contents in the command line without using inputs to scroll through it. Here is an example of using the cat command to view the Linux version by displaying the contents of the /proc/version file.


3 Answers

You could use xdg-open(1). It works on all freedesktop compliant desktops.

like image 154
Sahil Muthoo Avatar answered Oct 04 '22 13:10

Sahil Muthoo


The default programs for different mime-types are defined in /etc/mailcap and $HOME/.mailcap, indexed by file type and action (display, edit, print). The command line interface is run-mailcap. See also the manpages run-mailcap(1) and mailcap(5).

like image 28
thiton Avatar answered Oct 04 '22 12:10

thiton


It depends what desktop environment you're using in Linux. Gnome for example has a MIME database you can use to find out what to launch for a given file.

like image 36
Flexo Avatar answered Oct 04 '22 11:10

Flexo