Ubuntu – For loop with unix find

command linefindscripts

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 use while instead. To make sure it works with all file names, including those with spaces, newlines, tabs, backslashes or any other strange characters, use this:

find /home/family/Music -name '*.m4a' -print0 | while IFS= read -r -d '' file; do
     ffmpeg -i "$file" -acodec libvorbis -aq 6 -vn -ac 2 "${file%.m4a}.ogg";
done

Explanation

  • Note that I quoted *.mp4a which ensures that bash will not expand it before passing it to find. This is important for the cases where you have files that match that glob in your current directory.

  • The -print0, as you probably know, causes find 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 like for file in $(command). The options are (from help read):

     -r     do not allow backslashes to escape any characters
     -d delim   continue until the first character of DELIM is read, rather
        than newline
    

    Setting the delimiter to the empty string (-d '') makes read play nice with find's -print0.

  • "${file%.mp3}.ogg"; : This is simply to remove the .m4a suffix and replace it with .ogg so you get foo.ogg instead of foo.m4a.ogg.

The rest is the same as you had attempted so I'm guessing you understand it.

Related Question