linux - Remove non ASCII chars from multiple files recursive -
i have tons of files include non ascii characters , on complete file system. looking batch solution run 1 problem:
find . -name "*.yml" -print0 | while read -d $'\0' file; tr -cd '\11\12\15\40-\176' < "$file" > "$file"; done
this command should work fine, wouldn't there problem tr -cd doesn't work if use same input , output name. know alternative or how solve small issue?
got (no idea if looks nice or crappy):
find . -name "*.yml" -print0 | while read -d $'\0' file; cp "$file" "${file}.temp" && tr -cd '\11\12\15\40-\176' < "${file}.temp" > "$file" && rm "${file}.temp"; done
depending on file names processing, may want add ifs=
, -r
read command, see greg's bashfaq 001 details.
regarding question, use temporary file suggested carl or use sponge command the moreutils package. either:
find . -name "*.yml" -print0 | while read -r -d '' file; tr -cd '\11\12\15\40-\176' < "$file" > "${file}.tmp" \ && mv "${file}.tmp" "$file" done
or:
find . -name "*.yml" -print0 | while read -r -d '' file; tr -cd '\11\12\15\40-\176' < "$file" | sponge "$file" done
Comments
Post a Comment