I have a folder include many txt files and sub-folders which also contains many txt files.
I want to change all of the files' encoding automatically. Right now I know how to change one file using the command:
iconv -f gbk -t utf8 sample.txt > sample.wiki
I need the name of the file not changed and change its extension to .wiki. How can I achieve this automatically ?
You can iterate though the .txt
files, change encoding, save the output in .wiki
file of the same name using -o
option of iconv
and if successful remove the relevant .txt
file:
shopt -s globstar ## Enables recursive glob matching
for f in **/*.txt; do
iconv -f gbk -t utf8 -o "${f%.txt}".wiki "$f" && rm "$f"
done
Caveat:
If the input file is not in given source encoding or already in the target encoding, iconv
will return true without doing any operation, hence the .wiki
file will not be created and the .txt
file will be removed.
To work around this you can save the STDOUT after conversion in a variable and if the variable is not-empty only then save the output in the .wiki
file and remove the txt
file:
shopt -s globstar ## Enables recursive glob matching
for f in **/*.txt; do
out="$(iconv -f gbk -t utf8 "$f")"
[[ -n $out ]] && echo "$out" >"${f%.txt}".wiki && rm "$f"
done
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments