Setting default application for filetypes via CLI?

6,130

You can use mimeopen with -d option:

man mimeopen :

DESCRIPTION
   This script tries to determine the mimetype of a file and open it with
   the default desktop application. If no default application is
   configured the user is prompted with an "open with" menu in the
   terminal.


-d, --ask-default
       Let the user choose a new default program for given files.

Example:

mimeopen -d file.mp4

sample output:

Please choose a default application for files of type video/mp4

   1) VLC media player  (vlc)
   2) Other...

Verify it:

xdg-open file.mp4
Share:
6,130

Related videos on Youtube

user5104897
Author by

user5104897

Updated on September 18, 2022

Comments

  • user5104897
    user5104897 over 1 year

    I have 150 Debian Jessie machines that open ODS files in Gnumeric when double-clicked despite LibreOffice Calc being installed. I know it is possible to change this by right-clicking the ODS file and changing its default program from the Properties window, but getting 150 users to do this is not an option. They all use xfce4 and thunar.

    I need to do this via CLI so I can do it across all workstations remotely. I have looked in /usr/share/applications and ~/.local/share/application/mimetypes.list with no luck - comparing the files before and after changing it via GUI revealed no changes here.

    How can I use bash to make these workstations open ODS files with LibreOffice Calc by default?

    EDIT: Unlike the answers to this question, my Jessie installs do not have ~/.config/mimeapps.list or /usr/share/applications/defaults.list

    • Jeff Schaller
      Jeff Schaller about 7 years
      Just as a nit-pick, you're looking for a command-line solution; it doesn't matter that your shell is bash.
    • hyph
      hyph about 7 years
      can you check if your systems have the '/usr/share/applications/defaults.list' file? What window managers/file managers are on your systems?
    • hyph
      hyph about 7 years
      Possible duplicate of Set default pdf reader
    • user5104897
      user5104897 about 7 years
      @hyph I don't have a /usr/share/applications/defaults or ~/.conf/mimeapps.list. I'm using xfce and thunar.
    • hyph
      hyph about 7 years
      found another existing answer: unix.stackexchange.com/questions/23776/… have you seen that one and tried solutions from there?
    • user5104897
      user5104897 about 7 years
      @hyph Unfortunately my Jessie installs do not have a /usr/share/mime directory either, so that answer is not much help.
    • hyph
      hyph about 7 years
      do you have the update-mime-database command on your system?
    • hyph
      hyph about 7 years
      official documentation seems to be here: wiki.debian.org/MimeTypesSupport . Do you have a /usr/lib/mime directory?
    • u_Ltd.
      u_Ltd. over 4 years
      See also unix.stackexchange.com/questions/162742/… which also refers to /etc/mailcap* and ~/.mailcap
  • user5104897
    user5104897 about 7 years
    This works! You can also pipe input like so: echo "2" | mimeopen -d test.ods I have just one question: Is there a way to close the window this opens via bash script? If I run this over SSH it cannot open the window on my machine (X11Forwarding is off), so it hangs waiting for it to open. I can ^C out but I'm not sure how to send that input in a script.
  • GAD3R
    GAD3R about 7 years
    @user5104897 run ps aux | grep libre then run kill PID
  • user5104897
    user5104897 about 7 years
    I can do that manually, but how do I feed the PID to kill automatically? For example ps aux | grep libre | kill to kill all the PIDs left by the grep, or something like that.
  • GAD3R
    GAD3R about 7 years
    Only the PID witch belongs to Libre Office Calc. Also you can verify the X-forwarding on the remote machine e,g: cat /etc/ssh/sshd_config | grep X11Forwarding