Here is a one-liner:
ls -AQp | grep "\.mp3\"$" | xargs `xdg-mime query default audio/mpeg | grep -oP '.+(?=\.desktop)'`
As I have VLC installed (and as a default for mp3s), this opens all mp3-files in a directory with VLC for me. This is not any sort of "universal-solve-it-all-and-work-in-every-freaking-case", but it should work.
Explanation:
ls -AQp
lists "almost all" files, quoting filenames and appending slash to names of directories. Replace -p
with --file-type
if you wish to exclude symlinks as well. Quoting in case of spaces in filenames.
grep "\.mp3\"$"
selects only files that ends with ".mp3" (plus double-quote).
xargs
redirects the whole lot to program that following subshell returns.
subshell:
xdg-mime query default audio/mpeg
gives default app's name in format "app.desktop" for files whose mime is audio/mpeg
. You can check mimetype for any file in your environment with xdg-mime query filetype /path/to/file
. I got "audio/mpeg" for mp3-file.
grep -oP '.+(?=\.desktop)'
gets the "app" from "app.desktop".
If you're going to use it very frequently in a system that's not going to change much, you might want to shorten it to this:
ls -AQp | grep "\.mp3\"$" | xargs default_app
Where you replace default_app
with the actual program that opens with the files. You can figure out its name with this:
xdg-mime query default audio/mpeg | grep -oP '.+(?=\.desktop)'
xdg-open
wont work with this problem, because it accepts only one argument by design. If using
xargs -n1
, you're propably gona hit the wall with that the resulting app in question might open every file in a new instance, which might get ugly in more than one way.
xdg-open
should really be able to do this by default... just pass the argument list to the program. Come on freedesktop.org people! :-)