Change encoding of files (including the names)

Recently, at work, I had a bunch (too many to handle manually) of ISO-8859-1 encoded YAML-files, which I had to convert to UTF-8.

I knew that iconv took care of that, and with a for-loop I went through all files and converted them.

for file in *.yml; do
  iconv -t UTF-8 -f ISO-8859-1 "$file" > "new/$file"
done

The file contents was converted correct. But the file names however had question marks instead of some special characters (å, ä and ö). I solved that with this little hack.

for file in *.yml; do
  iconv -t UTF-8 -f ISO-8859-1 "$file" > "new/$(echo $file | iconv -t UTF-8 -f ISO-8859-1)"
done

Note that if you find yourself using this sometimes and want to make a script of it, you should probably refactor out the common call to iconv and make the formats available to specify as arguments.


blog comments powered by Disqus Back to Top

Fork me on GitHub