15

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.

1
  • 2
    xdg-open should really be able to do this by default... just pass the argument list to the program. Come on freedesktop.org people! :-) Mar 20, 2016 at 11:44

7 Answers 7

11

Indeed. You could use shell to get around this, like this:

ls *.mp3 | xargs -n 1 xdg-open

This is very simplistic though, and doesn't work for any special case (spaces, non-ascii characters). An improvement for this would be

ls -b *.mp3 | sed -e s+^+\"+ -e s+\$+\"+ | xargs -n 1 xdg-open

This is quite complex this way, though. A more robust, but simpler solution in this case would be to use find:

find -iname '*.mp3' -print0 | xargs -0 -n 1 xdg-open
7
  • The first doesn't work with files with whitespace in their name, and the second doesn't work at all (find: paths must precede expression: -).
    – Sparhawk
    Oct 24, 2013 at 13:22
  • 1
    If you read carefully, I wrote "if you want it more robust...", hence the first, simple version doesn't work with spaces indeed, but that was not the intention. The second solution does work with ubuntu; FYI, newer versions of find default the pathname to . Oct 25, 2013 at 14:29
  • I'm not sure that the lack of path is the problem in the second (despite the error message). I tried putting in . and got the same error message. In another directory, I get xdg-open: unexpected argument './foo.mp3' with or without ..
    – Sparhawk
    Oct 25, 2013 at 22:33
  • Also, I think that having spaces in names is not so uncommon a scenario that it should be disregarded, even in "simple" cases.
    – Sparhawk
    Oct 25, 2013 at 22:41
  • 1
    Doing ls *.mp3 | xargs xdg-open doesn't seem to work even without whitespaces in the filenames: xdg-open: unexpected argument
    – carlodef
    Feb 3, 2014 at 23:30
6

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.

#!/usr/bin/env bash
if [ $# -eq 0 ]; then
  xdg-open . &> /dev/null
else
  for file in "$@"; do
    xdg-open "$file" &> /dev/null
  done
fi

If you don't want to open the current directory with no argument, this retains the default behaviour, i.e. shows usage.

#!/usr/bin/env bash
if [ $# -eq 0 ]; then
  xdg-open &> /dev/null
else
  for file in "$@"; do
    xdg-open "$file" &> /dev/null
  done
fi

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.

2
  • Just wanted to say thanks. Have been using your second version for years. In .bashrc I made an alias o='[location to file with your script]'
    – Bart
    Jul 18, 2021 at 14:25
  • Thanks for the feedback @Bart! Glad I could help!
    – Sparhawk
    Jul 18, 2021 at 14:27
4

You can try:

ls *.mp3 | while read -r file; do xdg-open "$file"; done

ls *.mp3 wil list all mp3 files from the current directory, each one on its own line, and the output is piped to an while 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.

5
  • @karel I added the -r flag. I think this is necessary to correctly parse files with a `\` in their name.
    – Sparhawk
    Oct 26, 2013 at 1:13
  • 2
    umm... wont this open several instances of the same program?
    – Braiam
    Oct 26, 2013 at 1:38
  • @karel Yeah… my man read doesn't have any flags listed at all, too! I don't get an error in my testing, but that's probably because I created aaa and aa\a. In this case, xdg-open just tries to open aaa twice, and not aa\a. (Okay, it's getting a bit convoluted, but I think code should work for all situations. Perhaps find is a better option, to avoid the limitations of ls and read.) Also, how can it wreak havoc exactly? Isn't it only going to parse the output of ls?
    – Sparhawk
    Oct 26, 2013 at 2:06
  • @karel man read is reffering to the C function read (section 2 of the man). This read is a bash builtin command, so you should look in man bash somewhere at line 4632. @Sparhawk - thanks for edititng. Oct 26, 2013 at 7:00
  • @Braiam umm... this is not the case for mp3's and depends about the settings from the default application. The behavior is the same like when you open multiple files from Nautilus or your favorite file manager. Oct 26, 2013 at 7:28
2

I wrote this bash script to cover all the usage cases I could think of:

#!/bin/bash
set -euo pipefail; shopt -s failglob # bash strict mode

max=${max:-10} # Set default maximum if $max is not set
[[ ${all:-} ]] && max=$# # Set max to all files if $all is non-null

for file in "${@:1:$max}"; do
  xdg-open "$file"
done &>>~/.xsession-errors

Features:

  • Writes error output to ~/.xsession-errors rather than polluting the terminal or throwing it away
  • Defaults to opening a maximum of 10 files (to not overload screen / processor)
  • Can set a new maximum with max=20 open $(ls -Q)
  • Allow opening all files, eg all=1 open $(ls -Q)
0

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.

-1

You can use these commands

cd /path/to/source_folder

find . -type f -name *.mp3 -exec vlc {} \+

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.

4
  • Doesn't find...-exec run individual commands on each file? And also, if your music player supports multiple files as command line arguments, you could just use vlc *.mp3.
    – Sparhawk
    Oct 27, 2013 at 22:59
  • @Sparhawk Not with \+ at the end
    – A.B.
    Jul 8, 2015 at 14:20
  • @A.B. Ah yes, good point; I was wrong. However, as per my second sentence, it seems pointless to use find if you are going to hardcode vlc anyway. (The question suggests they are all in one directory.)
    – Sparhawk
    Jul 8, 2015 at 22:16
  • Just + is enough, no need for \+.
    – Grumbel
    Feb 15, 2018 at 15:46
-1

Use this command for mp3 files if you want to open files in VLC.

vlc /directory/*.mp3

Note: Use cvlc to use VLC without interface.

1
  • 3
    The question says "default program", so using VLC directly is a bit... pointless.
    – muru
    Sep 28, 2014 at 5:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .