Created
February 15, 2015 15:39
-
-
Save Darkwing371/aec59cf08965296473a8 to your computer and use it in GitHub Desktop.
sconv - Convert Files From One Charset Into Another (Linux Shell Script)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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."; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
→ Have your files converted.