Trying to do a mass convert from M4A to OGG in a large music collection, I have:
#!/bin/sh
for i in `find /home/family/Music -name *.m4a -print0`
#do ffmpeg -i "$i" -acodec libvorbis -aq 6 -vn -ac 2 "$i.ogg";
do echo $i
done
All of the files will have spaces in their names, the output for the above shows a single file like this:
/home/family/Music/The
Kooks/Inside
In
_
Inside
Out/06
You
Don't
Love
Me.m4a
Every space marks a new line, I thought -print0
would fix this?
Best Answer
That's one of the reasons why you never use a
for
loop to iterate over a command whose output can contain spaces. Especially if that output is a list of file names that can contain anything except/
and\0
. You have fallen into bash pitfall number 1. Always usewhile
instead. To make sure it works with all file names, including those with spaces, newlines, tabs, backslashes or any other strange characters, use this:Explanation
Note that I quoted
*.mp4a
which ensures that bash will not expand it before passing it tofind
. This is important for the cases where you have files that match that glob in your current directory.The
-print0
, as you probably know, causesfind
to separate its results with\0
instead of newlines.IFS=
: This sets the input field character to nothing, ensuring that no word splitting will take place.while read -r -d '' file
: This will iterate over the results, saving each as$file
, just likefor file in $(command)
. The options are (fromhelp read
):Setting the delimiter to the empty string (
-d ''
) makesread
play nice with find's-print0
."${file%.mp3}.ogg";
: This is simply to remove the.m4a
suffix and replace it with.ogg
so you getfoo.ogg
instead offoo.m4a.ogg
.The rest is the same as you had attempted so I'm guessing you understand it.