Bash Script – Open Any Kind of Application

applicationbash-scriptfile opening

I have made a Java program that could open any application. Suppose there is a file name "*.jpg", the it would allow the OS to recognize the type of application and then open the default application. Another example: Suppose the file's name is "*.flv", then it would open up the default media player just like when you click an icon in nautilus but the only difference was that it was in command line. Here is the java program for that:

import java.awt.*;
import java.io.*;
public class OpenFile{
    public static void main(String args[]){
      try{
          String filename = args[0];
          System.out.println(args[0]);
          Desktop.getDesktop().open(new File(filename));
      }
      catch(Exception e){
          System.out.println("Sorry an exception occured. Could not perform the operation.");
      }
    }
 }

Now this script leads to performance issues. Now I want to write a BASH script to do this task ??

Best Answer

There is already an external command for this. There is nothing new that needs to be written. The command is xdg-open. It will open a file based on its MIME type association. Here is an example:

xdg-open file.png
Related Question