0

I want to write a shellscript to convert all .ogg files in a directory to .mp3 files. The filenames contain spaces, so I use a lot of double quotes in the sequenco of commands. The command I use is

for FILE in "`ls -1 *.ogg`" ; do
    ROOTFILE=`printf "%s\n" "$FILE" | sed "s/ogg//" `
    ffmpeg -i "$FILE" "$ROOTFILE"mp3
done

But the all the filenames in the directory are now seen by ffmpeg as one string. How can I separate the individual filenames, while preserving the spaces in them?

1 Answers1

1

Summarising all the comments (written by Eric Renouf, steeldriver, and cas), the solution to your problem is

for f in *.ogg; do
    ffmpeg -i "$f" "${f%.ogg}.mp3"
done

This avoids a couple of issues with your attempt:

  • parsing ls’s output;
  • re-inventing basename (which itself can usually be replaced with shell string processing, as above).
Stephen Kitt
  • 434,908