views:

974

answers:

4

hi, I know I can convert a single file encoding under OSX using:

iconv -f ISO-8859-1 -t UTF-8 myfilename.xxx > myfilename-utf8.xxx

I have to convert a bunch of files with a specific extension, so I want to convert file encoding from ISO-8859-1 to UTF-8 for all *.ext files in folder /mydisk/myfolder

perhaps someobe know the syntax how to do this

thanks

ekke

A: 

You could write a script in any scripting language to iterate over every file in /mydisk/myfolder, check the extension with the regex [.(.*)$], and if it's "ext", run the following (or equivalent) from a system call.

"iconv -f ISO-8859-1 -t UTF-8" + file.getName() + ">" + file.getName() + "-utf8.xxx"

This would only be a few lines in Python, but I leave it as an exercise to the reader to go through the specifics of looking up directory iteration and regular expressions.

Stefan Kendall
+2  A: 

if your shell is bash, something like this

for files in /mydisk/myfolder/*.xxx
do
  iconv -f ISO-8859-1 -t UTF-8 "$files" "${files%.xxx}-utf8.xxx"
done
ghostdog74
A: 

If you want to do it recursively, you can use find(1):

find /mydisk/myfolder -name \*.xxx -type f | \
    (while read file; do
        iconv -f ISO-8859-1 -t UTF-8 -i "$file" -o "${file%.xxx}-utf8.xxx
    done)

Note that I've used | while read instead of the -exec option of find (or piping into xargs) because of the manipulations we need to do with the filename, namely, chopping off the .xxx extension (using ${file%.xxx}) and adding -utf8.xxx.

Adam Rosenfield
+1  A: 

Adam' comment showed me the way how to resolve it, but this was the only syntax I made it work:

find /mydisk/myfolder -name *.xxx -type f | \ (while read file; do iconv -f ISO-8859-1 -t UTF-8 "$file" > "${file%.xxx}-utf8.xxx"; done);

-i ... -o ... doesnt work, but >

thx again

ekke

ekkescorner