My Windows files have wrong encoding on Linux. How do I fix them all?

Quick Linux Tip #45:

Try: find . -name '*.txt' -exec sh -c 'iconv -f WINDOWS-1252 -t UTF-8 "$1" -o "$1.utf8" && mv "$1.utf8" "$1"' _ {} \;

Info: iconv converts text encodings (-f=from, -t=to). Wrapping in sh -c enables chaining iconv and mv per file. The _ acts as a placeholder for $0 in the subshell.

Examples:

  • $ iconv -f UTF-16 -t UTF-8 windows.txt -o linux.txt  # Convert single file
  • $ iconv -l | grep -i utf  # List all supported UTF encodings
  • $ dos2unix *.txt  # Fix CRLF line endings only

Note: Always test on a backup copy first. Use iconv -l to see all supported charsets. Use dos2unix if you only need to fix line endings (CRLF to LF).




LinuxTeck.com
linuxteck@ubuntu:~$ file -i document.txt
document.txt: text/plain; charset=iso-8859-1

linuxteck@ubuntu:~$ find . -name '*.txt' -exec sh -c 'iconv -f CP1252 -t UTF-8 "$1" -o "$1.tmp" && mv "$1.tmp" "$1"' _ {} \;

linuxteck@ubuntu:~$ file -i document.txt
document.txt: text/plain; charset=utf-8
linuxteck@ubuntu:~$

PREVIOUS ARTICLE Quick Linux Tip #44: My connections feel unreliable. How do I check for hidden packet loss? NEXT ARTICLE Quick Linux Tip #46: I'm about to make risky changes. How do I create a rollback point?
About John Britto

John Britto Founder & Chief-Editor @LinuxTeck. A Computer Geek and Linux Intellectual having more than 20+ years of experience in Linux and Open Source technologies.

View all posts by John Britto →

Leave a Reply

Your email address will not be published.