I'm trying to find all text files which have the encoding iso-8859-1 and convert these to UTF-8. My attempt so far is:
find . -name '*.txt' | xargs grep 'iso-8859-1' | cut -d ':' -f1 |
xargs iconv -f ISO-8859-1 -t UTF-8 {} > {}.converted
The (obvious) problem is that the last variable substitution won't work, since {}
occurs after the redirection, and doesn't belong to xargs
. As is I only get one file called {}.converted
, not a.txt.converted
, b.txt.converted
etc. How can I make this work?
Note: I'm doing this on Cygwin, where iconv doesn't seem to support -o
.
If you have GNU Parallel http://www.gnu.org/software/parallel/ installed you can do this:
find . -name '*.txt' | parallel grep -il iso-8859-1 | parallel iconv -f ISO-8859-1 -t UTF-8 {} \> {}.converted
You can install GNU Parallel simply by:
wget http://git.savannah.gnu.org/cgit/parallel.git/plain/src/parallel
chmod 755 parallel
cp parallel sem
Watch the intro videos for GNU Parallel to learn more: https://www.youtube.com/playlist?list=PL284C9FF2488BC6D1
How about a for
loop like:
for file in `find . -name '*.txt' | xargs grep 'iso-8859-1' | cut -d ':' -f1`; do
iconv -f ISO-8859-1 -t UTF-8 $file > $file.converted
done
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With