How to launch the correct associated program for any file from the console

How to launch the correct associated program for any file from the console

Let’s say you have a .png file, a .txt file and an .mp3 file. If you are using Konqueror (or Nautilus under Gnome), just double click, the program you’ve chosen for this type of file will be launched: a picture viewer for the .png file, a text viewer for the .txt file, and a multimedia player for the .mp3 file.

Now, you’re using the console, and you don’t want to bother wondering which program you want to launch if you want to view a file. You can’t “execute” it either, because a .png file is just not executable (it’s not a program nor a script!).

What happens when you double-click a file in Konqueror/Nautilus is : “okay, what’s the file type? got it, I will launch the right program and ask it to view the file”. There’s a way to do that using the console, too.

Without it, if you want to listen to your .mp3 file, you can type:

$ xmms intro.mp3

But it implies you have to know each program name for each type file. Not very handy. Here comes the solution.

# Under KDE:
$ kfmclient exec the_filename_goes_here

# Under Gnome:
$ gnome-open the_filename_goes_here

In both cases, the correct program will be launched to view the file, according to your KDE/Gnome preferences, yay! If you use this command often, you can set a shorter alias in your .bashrc file (“kfmclient exec” is a bit long to type, and Linux is about efficiency, right?), adding the following line to it:

alias kx="kfmclient exec"
alias gx="gnome-open"
This post is licensed under CC BY 4.0 by the author.