I tried to open all my .mp3 files from a folder using xdg-open
but I found out it opens just one! So I searched a little but there was not such a question! I found "evince" but apparently it open text files and gnome-open also opens one file.
I want to open all files of the same format in a folder from the terminal. I'm new to Ubuntu so please explain a little more.
Indeed. You could use shell to get around this, like this:
This is very simplistic though, and doesn't work for any special case (spaces, non-ascii characters). An improvement for this would be
This is quite complex this way, though. A more robust, but simpler solution in this case would be to use find:
I wrote a small script
/usr/local/bin/o
, although you could just call it/usr/local/bin/xdg-open
and replace the default command if you wanted (assuming your$PATH
gives it priority). Also, if it is given no argument, this script will open the current directory instead.If you don't want to open the current directory with no argument, this retains the default behaviour, i.e. shows usage.
N.B. this is agnostic about the default program's ability to parse multiple arguments, but instead will call each command once for each argument. I don't think there's an elegant way around this, since users may want to
xdg-open
different kinds of files, and some commands will not take multiple arguments anyway.You can try:
ls *.mp3
wil list all mp3 files from the current directory, each one on its own line, and the output is piped to anwhile
loop witch read the content of each line and it will open that content (which is the name of a mp3 file in this case) in its default application.I wrote this
bash
script to cover all the usage cases I could think of:Features:
max=20 open $(ls -Q)
all=1 open $(ls -Q)
Here is a one-liner:
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 isaudio/mpeg
. You can check mimetype for any file in your environment withxdg-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:
Where you replace
default_app
with the actual program that opens with the files. You can figure out its name with this:xdg-open
wont work with this problem, because it accepts only one argument by design. If usingxargs -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.You can use these commands
only if your music player supports multiple files as command line arguments. Replace
vlc
with your music player of choice.This works with RhythmBox and VLC in my testing.
Use this command for mp3 files if you want to open files in VLC.
Note: Use
cvlc
to use VLC without interface.