Skip to content

Instantly share code, notes, and snippets.

@Darkwing371
Created February 15, 2015 15:39
Show Gist options
  • Save Darkwing371/aec59cf08965296473a8 to your computer and use it in GitHub Desktop.
Save Darkwing371/aec59cf08965296473a8 to your computer and use it in GitHub Desktop.
sconv - Convert Files From One Charset Into Another (Linux Shell Script)
#!/bin/sh
# sconv - Sophisticated Charset Converter
FROM=iso-8859-1
TO=utf-8
IN=(html htm php css scss sass txt csv)
TOTAL=0
CONVERTED=0
echo "";
echo "Converting files from $FROM to $TO.";
echo "";
# Cycle through all files
for FILE in $(find . -type f -nowarn); do
((TOTAL++))
# Get file name
FNAME="${FILE%.*}";
# Get file extention
EXT="${FILE##*.}";
# Make it lowercase
EXT=${EXT,,};
# Check file extention if we are interested in it
for i in "${IN[@]}"; do
if [ "$i" == "$EXT" ]; then
# Get charset of file
CHARSET=$(file -b --mime-encoding "$FILE");
# Compare charset if we need to perform conversion
if [ "$CHARSET" == "$FROM" ]; then
((CONVERTED++))
# Convert file (make new file)
$(iconv -f "$FROM" -t "$TO" $FILE > $FNAME.utf8);
# Overwrite old file with new file (http://stackoverflow.com/a/a24836200)
$(mv "$FNAME".utf8 "$FILE");
# Output some stuff
echo $FNAME"."$EXT":" $CHARSET "->" $TO;
# TODO: Maybe also convert file name itself to UTF-8?
# http://mindspill.net/computing/linux-notes/determine-and-change-file-character-encoding/
# like: $(convmv -f "$FROM" -t "$TO" --notest $FILE > $FNAME.utf8);
# Overwrite file again (?)
# ...
fi
fi
done
done
echo "";
echo "Done!";
echo "Converted $CONVERTED of $TOTAL files.";
@Darkwing371
Copy link
Author

  1. Create sconv.sh in your desired directory (often your web root).
  2. Give it executable rights.
  3. Copy Gist in there (and maybe make alterations to charsets and/or file extentions).
  4. Execute it by typing “/. sh.conv.sh”.

→ Have your files converted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment