Skip to content

Instantly share code, notes, and snippets.

@kurahaupo
Last active April 30, 2017 03:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kurahaupo/230dca8b32823e7d2f4fcf3137eeafd6 to your computer and use it in GitHub Desktop.
Save kurahaupo/230dca8b32823e7d2f4fcf3137eeafd6 to your computer and use it in GitHub Desktop.
#!/bin/bash
shopt -s globstar
sdir=$HOME/Documents/website
tdir=$HOME/tmp/transcoded-website
scode=WINDOWS-1252
tcode=UTF-8
mkdir -vp "$tdir"
cp -al "$sdir/." "$tdir" || exit
for f in "$tdir"/**/*.shtml \
"$tdir"/**/*.txt \
"$tdir"/**/*.p[lm]
do
# if you want to do some tests, put those here, e.g.:
# [[ -s $f ]] || continue # skip things that aren't plain files
# file "$f" | grep -qs "Text" || continue # skip things that don't contain text
# file "$f" | grep -qsE "Unicode|UTF" && continue # skip things that are already Unicode or UTF-something
g=$f.NEW$$
iconv --from-code=$sc --to-code=$tc <"$f" >"$g" || exit
if cmp -s "$f" "$g" ; then
# the transcoded file is identical to the original, so don't replace it
rm "$g"
else
touch --ref="$f" "$g" # optional -- set timestamp to match original file
chmod --ref="$f" "$g" # optional -- set permissions to match original file
mv -T "$g" "$f"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment