Setting default application for filetypes via CLI?
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
Related videos on Youtube
user5104897
Updated on September 18, 2022Comments
-
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 about 7 yearsJust as a nit-pick, you're looking for a command-line solution; it doesn't matter that your shell is bash.
-
hyph about 7 yearscan you check if your systems have the '/usr/share/applications/defaults.list' file? What window managers/file managers are on your systems?
-
hyph about 7 yearsPossible duplicate of Set default pdf reader
-
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 about 7 yearsfound another existing answer: unix.stackexchange.com/questions/23776/… have you seen that one and tried solutions from there?
-
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 about 7 yearsdo you have the
update-mime-database
command on your system? -
hyph about 7 yearsofficial documentation seems to be here: wiki.debian.org/MimeTypesSupport . Do you have a /usr/lib/mime directory?
-
u_Ltd. over 4 yearsSee also unix.stackexchange.com/questions/162742/… which also refers to
/etc/mailcap
* and~/.mailcap
-
-
user5104897 about 7 yearsThis 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 about 7 years@user5104897 run
ps aux | grep libre
then runkill PID
-
user5104897 about 7 yearsI can do that manually, but how do I feed the PID to
kill
automatically? For exampleps aux | grep libre | kill
to kill all the PIDs left by the grep, or something like that. -
GAD3R about 7 yearsOnly 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