how to open multiple files with the default program from terminal?
Solution 1:
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
Solution 2:
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.