Ubuntu – how to open multiple files with the default program from terminal

xdg-open

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.

Best Answer

  • 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