0

I want to convert a massive bunch of wmf files (120860!) to svg in many subfolders (27 subfolder with many sub-subfolders each). I have this bash script that does

for i in `find -iname "*.wmf"`; do
  uniconvertor ${i} ${i%.wmf}.svg
done

but it never converts everything, it always stops somewhere in the middle. Some subfolders get converted, some not, some only partially. Even if I start the shell script in all 27 first-level subfolders, only part of the files get converted.

What could be the problem?

  • ... any spaces in those filenames? – steeldriver Feb 14 '16 at 15:19
  • no, but spaces in the folder names - that's probably the problem. – HinzundKunz Feb 14 '16 at 15:29
1

If you don't have line breaks in the names of your files or folders you should use a while instead of a for loop:

find -iname "*.wmf" | while read file; do
    uniconvertor "$file" "${i%.wmf}.svg" 
done

This avoids problems with spaces in names and with too much arguments when expanding the ...

| improve this answer | |
  • I replaced all spaces with underscores and it worked, but your solution probably is much more elegant... ;-) – HinzundKunz Feb 29 '16 at 11:40
0

You must surround your find ... command with $( ) to substitute the output, thusly:

for i in $( find . -type f -iname '*.wmv` ) ; do ...

However, you're causing Bash to produce (and store internally) a list of 120,860 filenames before you process the first one. Also (you haven't described the format of the filenames), this technique mishandles filenames with Spaces in them, e.g. A Big File.wmf.

Read man find, especially, about --print0, read man xargs, especially about -0, read man bash and wrap your command in a script that runs uniconverter on each of its arguments, and use something like:

find . -type f -iname '*.wmv' -print0 | xargs -0 thescript 

Be sure to read man bash and be very aware of any Spaces in your filenames.

@steeldriver: TQ for pointing out my error. Reading man bash shows:

 Enclosing characters in double quotes preserves the  literal  value  of
       all  characters  within the quotes, with the exception of $, `, \, and,
       when history expansion is enabled, !.  The characters $  and  `  retain
       their  special meaning within double quotes.  The backslash retains its
       special meaning only when followed by one of the following  characters:
       $,  `,  ", \, or <newline>.  A double quote may be quoted within double
       quotes by preceding it with a backslash.

So I overworried about Shell expansion.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.